Advertisement
dasmalle

PlayerMovement.cs

Mar 18th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 KB | None | 0 0
  1. ?using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerMovement : MonoBehaviour {
  6.  
  7. public float speed; //this is the speed our player will move
  8. private Rigidbody2D rb; //variable to store Rigidbody2D component
  9. private float moveInput; //this store the input value
  10.  
  11. public float jumpForce; //force with which player jump
  12. private bool isGrounded; //this variable will tell if our player is grounded or not
  13. public Transform feetPos; //this variable will store reference to transform from where we will create a circle
  14. public float circleRadius; //radius of circle
  15. public LayerMask whatIsGround; //layer our ground will have.
  16.  
  17. public float jumpTime; //time till which we will apply jump force
  18. private float jumpTimeCounter; //time to count how long player has pressed jump key
  19. private bool isJumping; //bool to tell if player is jumping or not
  20.  
  21. // Use this for initialization
  22. void Start ()
  23. {
  24. rb = GetComponent<Rigidbody2D>(); //get reference to Rigidbody2D component
  25. }
  26.  
  27. //as we are going to use physics for our player , we must use FixedUpdate for it
  28. void FixedUpdate ()
  29. {
  30. //the moveInput will be 1 when we press right key and -1 for left key
  31. moveInput = Input.GetAxis("Horizontal");
  32. if (moveInput > 0) //moving towards right side
  33. transform.eulerAngles = new Vector3(0, 0, 0);
  34. else if (moveInput < 0) //moving towards left side
  35. transform.eulerAngles = new Vector3(0, 180, 0);
  36. //here we set our player x velocity and y will not ne changed
  37. rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
  38. }
  39.  
  40. private void Update()
  41. {
  42. //here we set the isGrounded
  43. isGrounded = Physics2D.OverlapCircle(feetPos.position, circleRadius, whatIsGround);
  44. //we check if isGrounded is true and we pressed Space button
  45. if (isGrounded == true && Input.GetKeyDown(KeyCode.Space))
  46. {
  47. isJumping = true; //we set isJumping to true
  48. jumpTimeCounter = jumpTime; //set jumpTimeCounter
  49. rb.velocity = Vector2.up * jumpForce; //add velocity to player
  50. }
  51.  
  52. //if Space key is pressed and isJumping is true
  53. if (Input.GetKey(KeyCode.Space) && isJumping == true)
  54. {
  55. if (jumpTimeCounter > 0) //we check if jumpTimeCounter is more than 0
  56. {
  57. rb.velocity = Vector2.up * jumpForce; //add velocity
  58. jumpTimeCounter -= Time.deltaTime; //reduce jumpTimeCounter by Time.deltaTime
  59. }
  60. else //if jumpTimeCounter is less than 0
  61. {
  62. isJumping = false; //set isJumping to false
  63. }
  64. }
  65.  
  66. if (Input.GetKeyUp(KeyCode.Space)) //if we unpress the Space key
  67. {
  68. isJumping = false; //set isJumping to false
  69. }
  70.  
  71. }
  72.  
  73.  
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement