Advertisement
kadyr

Untitled

Aug 28th, 2021
1,054
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.01 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4.  
  5. public class GuyMove : MonoBehaviour
  6. {
  7.     private Rigidbody rb; // компонент Rigidbody
  8.     public float speed = 5;
  9.     Vector3 direction;
  10.     Vector3 startPosition; // начальное положение
  11.    
  12.     [SerializeField] // this
  13.     private AudioSource jumpAudioSource; //this
  14.    
  15.     private Animator playerAniamtor; //this 2
  16.     void Start()
  17.     {
  18.         startPosition = transform.position;
  19.         rb = GetComponent<Rigidbody>();
  20.         playerAniamtor = GetComponent<Animator>(); // this 2
  21.     }
  22.     private void Update()
  23.     {
  24.         if (finish == false)
  25.         {
  26.             time += Time.deltaTime;
  27.             timeText.text = time.ToString();
  28.         }
  29.  
  30.  
  31.  
  32.         if (isGrouned())
  33.         {
  34.             //позволять работать анимации движения
  35.             if (Input.GetKeyDown(KeyCode.Space))
  36.             {
  37.                 //вызов триггера прыжка
  38.                 jumpAudioSource.Play(); // this!
  39.                 rb.AddForce(new Vector3(0, 8, 0), ForceMode.Impulse);
  40.             }
  41.         }
  42.         //Вызывать изграунд у аниматора
  43.        
  44.         if (transform.position.y < -10)
  45.         {
  46.             transform.position = startPosition;
  47.         }
  48.     }
  49.  
  50.     private bool isGrouned()
  51.     {
  52.         return Physics.Raycast(transform.position, -Vector3.up, 2 + 0.1f);
  53.     }
  54.     void FixedUpdate()
  55.     {
  56.         float horizontal = Input.GetAxis("Horizontal");
  57.         float vertical = Input.GetAxis("Vertical");
  58.  
  59.         direction = transform.TransformDirection(horizontal, 0, vertical);
  60.         rb.MovePosition(transform.position + speed * direction * Time.deltaTime);
  61.     }
  62.  
  63.     float time = 0;
  64.     bool finish = false;
  65.     [SerializeField] Text timeText;
  66.  
  67.     private void OnTriggerEnter(Collider other)
  68.     {
  69.         if (other.tag == "Finish")
  70.         {
  71.             finish = true;
  72.         }
  73.     }
  74. }
  75.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement