Advertisement
Guest User

Untitled

a guest
Dec 11th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class PlayerScript : MonoBehaviour {
  5.  
  6. [HideInInspector] public bool facingRight = true;
  7. [HideInInspector] public bool jump = false;
  8. public float moveForce = 265f;
  9. public float maxSpeed = 5f;
  10. public float jumpForce = 1000f;
  11. public Transform groundCheck;
  12. public GameObject heroShadow;
  13.  
  14. public AudioSource audioSource;
  15. public AudioClip footStepSound;
  16.  
  17.  
  18.  
  19. private bool grounded = false;
  20. private Animator anim;
  21. private Rigidbody2D rb2d;
  22.  
  23.  
  24. // Use this for initialization
  25. void Awake ()
  26. {
  27. anim = GetComponent<Animator>();
  28. rb2d = GetComponent<Rigidbody2D>();
  29. audioSource = GetComponent<AudioSource> ();
  30.  
  31. }
  32.  
  33. // Update is called once per frame
  34. void Update ()
  35. {
  36. grounded = Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground"));
  37.  
  38. if (Input.GetButtonDown ("Jump") && grounded) {
  39. jump = true;
  40. anim.SetBool ("isJumping", true);
  41. }
  42.  
  43.  
  44. if (grounded == false) {
  45. heroShadow.gameObject.SetActive (false);
  46. anim.SetBool ("isJumping", true);
  47. anim.SetBool ("isWalking", false);
  48. anim.SetBool ("isIdle", false);
  49.  
  50. } else {
  51. heroShadow.gameObject.SetActive (true);
  52. anim.SetBool ("isJumping", false);
  53. grounded = true;
  54. }
  55.  
  56. }
  57.  
  58. void FixedUpdate()
  59. {
  60. float h = Input.GetAxis("Horizontal");
  61. float v = Input.GetAxis ("Vertical");
  62. /*
  63. if (v != 0f) {
  64. anim.SetBool ("isJumping", true);
  65. } else {
  66. anim.SetBool ("isJumping", false);
  67. }
  68. */
  69.  
  70. if (h != 0f && jump == true && grounded == false) {
  71. anim.SetBool ("isJumping", true);
  72. anim.SetBool ("isWalking", false);
  73. anim.SetBool ("isIdle", false);
  74. }
  75. else if (h != 0f && jump == false && grounded) {
  76. anim.SetBool ("isWalking", true);
  77. anim.SetBool ("isJumping", false);
  78. anim.SetBool ("isIdle", false);
  79.  
  80. audioSource.PlayOneShot(footStepSound);
  81.  
  82.  
  83. } else if (h <= 1f && jump == false && grounded) {
  84. anim.SetBool ("isIdle", true);
  85. anim.SetBool ("isWalking", false);
  86. anim.SetBool ("isJumping", false);
  87. } else if (h <= 1f && jump == true && grounded == false) {
  88. anim.SetBool ("isJumping", true);
  89. anim.SetBool ("isIdle", false);
  90. anim.SetBool ("isWalking", false);
  91. } else if (v != 0f && grounded == false) {
  92. anim.SetBool ("isJumping", true);
  93. anim.SetBool ("isIdle", false);
  94. anim.SetBool ("isWalking", false);
  95. }
  96.  
  97. if (v != 0f && h != 0f && jump == true && grounded == false) {
  98. anim.SetBool ("isJumping", true);
  99. anim.SetBool ("isWalking", false);
  100. anim.SetBool ("isIdle", false);
  101. }
  102.  
  103.  
  104.  
  105. anim.SetFloat("Speed", Mathf.Abs(h));
  106.  
  107. if (h * rb2d.velocity.x < maxSpeed)
  108. rb2d.AddForce (Vector2.right * h * moveForce);
  109.  
  110. if (Mathf.Abs (rb2d.velocity.x) > maxSpeed) {
  111. rb2d.velocity = new Vector2 (Mathf.Sign (rb2d.velocity.x) * maxSpeed, rb2d.velocity.y);
  112.  
  113. }
  114. if (h < 0 && !facingRight)
  115. Flip ();
  116. else if (h > 0 && facingRight)
  117. Flip ();
  118.  
  119. if (jump) {
  120. //anim.SetTrigger("Jump");
  121. anim.SetBool ("isJumping", true);
  122. rb2d.AddForce (new Vector2 (0f, jumpForce));
  123. //StartCoroutine (JumpLanding ());
  124. jump = false;
  125.  
  126. }
  127. }
  128.  
  129.  
  130. void Flip()
  131. {
  132. facingRight = !facingRight;
  133. Vector3 theScale = transform.localScale;
  134. theScale.x *= -1;
  135. transform.localScale = theScale;
  136. }
  137.  
  138. /*IEnumerator JumpLanding() {
  139. yield return new WaitForSeconds (0.01f);
  140. anim.SetBool ("isJumping", false);
  141. }*/
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement