Advertisement
kadyr

Untitled

Aug 21st, 2021
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 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. characterAnimator.SetFloat("directionValue",direction.magnitude);
  55. if (Input.GetKeyDown(KeyCode.Space))
  56. {
  57. characterAnimator.SetTrigger("jumpTrigger");
  58. rb.AddForce(new Vector3(0, jumpSpeed, 0), ForceMode.Impulse);
  59. jumpSource.Play();
  60. }
  61. }
  62. characterAnimator.SetBool("IsGrounded",isGrounded());
  63.  
  64. }
  65.  
  66. private bool isGrounded()
  67. {
  68. return Physics.Raycast(transform.position, -Vector3.up, 2 + 0.1f);
  69. }
  70. private void FixedUpdate()
  71. {
  72. float horizontal = Input.GetAxis("Horizontal");
  73. float vertical = Input.GetAxis("Vertical");
  74. direction = transform.TransformDirection(new Vector3(horizontal, 0, vertical));
  75. rb.MovePosition(transform.position + direction * speed * Time.deltaTime);
  76. }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement