Advertisement
kadyr

Untitled

Aug 21st, 2021
1,627
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.05 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3.  
  4. public class GuyMove : MonoBehaviour
  5. {
  6.     public AudioSource jumpSource;
  7.  
  8.     [SerializeField]
  9.     private float speed = 5f;
  10.  
  11.     [SerializeField]
  12.     private float jumpSpeed = 10f;
  13.  
  14.     //переменная, отвечающая за нашу физику
  15.     private Rigidbody rb;
  16.  
  17.     public Text ScoreTextTime;
  18.     private float scoreTime;
  19.  
  20.     private Vector3 direction;
  21.  
  22.     //точка респаун
  23.     private Vector3 startPosition;
  24.    
  25.     private Animator characterAnimator;
  26.  
  27.     private bool finishGame;
  28.  
  29.     private void Start()
  30.     {
  31.         rb = GetComponent<Rigidbody>();
  32.         characterAnimator = GetComponent<Animator>();
  33.         startPosition = transform.position;
  34.     }
  35.  
  36.     public void ResetPosition()
  37.     {
  38.         transform.position = startPosition;
  39.     }
  40.  
  41.     private void Update()
  42.     {
  43.         if (!finishGame)
  44.         {
  45.             scoreTime += Time.deltaTime;
  46.             ScoreTextTime.text = scoreTime.ToString();
  47.         }
  48.         //Работа с анимациями
  49.        
  50.  
  51.  
  52.         if (!isGrounded())
  53.         {
  54.             if (direction.magnitude > 0)
  55.             {
  56.                 characterAnimator.SetFloat("directionValue",direction.magnitude);
  57.             }
  58.             if (Input.GetKeyDown(KeyCode.Space))
  59.             {
  60.                 characterAnimator.SetTrigger("jumpTrigger");
  61.                 rb.AddForce(new Vector3(0, jumpSpeed, 0), ForceMode.Impulse);
  62.                 jumpSource.Play();
  63.             }
  64.         }
  65.         characterAnimator.SetBool("IsJumped", isGrounded());
  66.  
  67.     }
  68.  
  69.     private bool isGrounded()
  70.     {
  71.         return Physics.Raycast(transform.position, -Vector3.up, 1 + 0.1f);
  72.     }
  73.     private void FixedUpdate()
  74.     {
  75.         float horizontal = Input.GetAxis("Horizontal");
  76.         float vertical = Input.GetAxis("Vertical");
  77.         direction = transform.TransformDirection(new Vector3(horizontal, 0, vertical));
  78.         rb.MovePosition(transform.position + direction * speed * Time.deltaTime);
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement