Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerInput : MonoBehaviour
  6. {
  7. public GameObject Barrier1;
  8. public GameObject Barrier2;
  9.  
  10. private Animator anim;
  11. private Rigidbody rb;
  12.  
  13. private float RunningSpeed = 8.5f;
  14. private float BackwardSpeed = 1.0f;
  15. private float IdleSpeed = 3.5f;
  16. private bool CurRunning = false;
  17. private bool IsGrounded = true;
  18.  
  19. private float JumpHeight = 24.0f;
  20. public float MovementSpeed;
  21. // Start is called before the first frame update
  22. void Start()
  23. {
  24. anim = GetComponent<Animator>();
  25.  
  26. rb = GetComponent<Rigidbody>();
  27. }
  28.  
  29. // Update is called once per frame
  30. void Update()
  31. {
  32. if (Input.GetKey("escape"))
  33. {
  34. Application.Quit();
  35. }
  36.  
  37. if (Input.GetKey("d"))
  38. {
  39. MovementSpeed = 1 * Time.deltaTime * RunningSpeed;
  40.  
  41. transform.Translate(MovementSpeed, 0, 0);
  42.  
  43. if(anim.GetBool("Move") == false)
  44. {
  45. StartRunAnimation();
  46. }
  47.  
  48. CurRunning = true;
  49. }
  50. else if (Input.GetKey("a"))
  51. {
  52. MovementSpeed = 1 * Time.deltaTime * BackwardSpeed;
  53.  
  54. transform.Translate(MovementSpeed, 0, 0);
  55.  
  56. if(anim.GetBool("Move") == true)
  57. {
  58. StopRunAnimation();
  59. }
  60.  
  61. CurRunning = false;
  62. }
  63. else
  64. {
  65. MovementSpeed = 1 * Time.deltaTime * IdleSpeed;
  66.  
  67. transform.Translate(MovementSpeed, 0, 0);
  68.  
  69. if (anim.GetBool("Move") == true)
  70. {
  71. StopRunAnimation();
  72. }
  73.  
  74. CurRunning = false;
  75. }
  76.  
  77. if (Input.GetKeyDown("space") && IsGrounded)
  78. {
  79. rb.AddForce(new Vector3(0, JumpHeight, 0), ForceMode.Impulse);
  80.  
  81. IsGrounded = false;
  82.  
  83. if (Input.GetKey("d"))
  84. {
  85. anim.Play("RunningToJump");
  86. }
  87. else
  88. {
  89. anim.Play("IdleToJump");
  90. }
  91. }
  92. }
  93.  
  94. void StartRunAnimation()
  95. {
  96. anim.SetBool("Move", true);
  97. }
  98.  
  99. void StopRunAnimation()
  100. {
  101. anim.SetBool("Move", false);
  102. }
  103.  
  104. private void OnCollisionEnter(Collision collision)
  105. {
  106. if(collision.collider.gameObject.name != "GrassPlatform" && collision.collider.gameObject != Barrier1 && collision.collider.gameObject != Barrier2)
  107. {
  108. if(CurRunning == true)
  109. {
  110. anim.Play("HitWhileRunning");
  111. }
  112. else
  113. {
  114. anim.Play("GetHit");
  115. }
  116. }
  117. else
  118. {
  119. IsGrounded = true;
  120. }
  121. }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement