Advertisement
kadyr

Untitled

Aug 28th, 2021
1,053
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.99 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.         if (isGrounded())
  31.         {
  32.             playerAniamtor.SetFloat("directionValue",direction.magnitude);
  33.             if (Input.GetKeyDown(KeyCode.Space))
  34.             {
  35.                 playerAniamtor.SetTrigger("JumpTrigger");
  36.                 jumpAudioSource.Play();
  37.                 rb.AddForce(new Vector3(0, 20, 0), ForceMode.Impulse);
  38.             }
  39.         }
  40.         playerAniamtor.SetBool("isGrounded",isGrounded());
  41.        
  42.         if (transform.position.y < -10)
  43.         {
  44.             transform.position = startPosition;
  45.         }
  46.     }
  47.  
  48.     private bool isGrounded()
  49.     {
  50.         return Physics.Raycast(transform.position, -Vector3.up, 2 + 0.1f);
  51.     }
  52.     void FixedUpdate()
  53.     {
  54.         float horizontal = Input.GetAxis("Horizontal");
  55.         float vertical = Input.GetAxis("Vertical");
  56.  
  57.         direction = transform.TransformDirection(horizontal, 0, vertical);
  58.         rb.MovePosition(transform.position + speed * direction * Time.deltaTime);
  59.     }
  60.  
  61.     float time = 0;
  62.     bool finish = false;
  63.     [SerializeField] Text timeText;
  64.  
  65.     private void OnTriggerEnter(Collider other)
  66.     {
  67.         if (other.tag == "Finish")
  68.         {
  69.             finish = true;
  70.         }
  71.     }
  72. }
  73.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement