Advertisement
Guest User

Untitled

a guest
Apr 1st, 2020
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. public class playerMovement : MonoBehaviour
  2. {
  3.  
  4. public float f_MoveSpeed = 5f; //! Defines the move speed for the player
  5. public float f_JumpHeight = 2f; //! Defines the jump height for the player
  6. private Vector3 vec_Input; //! A vector 3 to store our input
  7. private Rigidbody rb; //! Rigidbody variable stored on our player
  8.  
  9. public Transform groundChecker; //! A transform which will collide with the ground
  10. public float f_GroundCheckRadius = 0.4f; //! The size of the sphere checking if the player is on the ground
  11. public LayerMask groundMask; //! The mask used for the ground
  12. [SerializeField]private bool grounded; //! A bool to store if the player is grounded or not
  13. public VariableJoystick joyStick; //! A variable for the joystick
  14.  
  15. private void Start()
  16. {
  17. rb = GetComponent<Rigidbody>();
  18. }
  19.  
  20. void FixedUpdate()
  21. {
  22. movement();
  23. jump();
  24. }
  25.  
  26. private void Update()
  27. {
  28. checkGround();
  29. }
  30.  
  31. void movement()
  32. {
  33. #if UNITY_STANDALONE_WIN || UNITY_EDITOR
  34. vec_Input = new Vector3(Input.GetAxis("Horizontal") * f_MoveSpeed, rb.velocity.y, Input.GetAxis("Vertical") * f_MoveSpeed);
  35. rb.velocity = transform.TransformDirection(vec_Input);
  36. #endif
  37.  
  38. #if UNITY_ANDROID
  39. vec_Input = new Vector3(joyStick.Horizontal * f_MoveSpeed, rb.velocity.y, joyStick.Vertical * f_MoveSpeed);
  40. rb.velocity = transform.TransformDirection(vec_Input);
  41. #endif
  42.  
  43. }
  44.  
  45. void jump()
  46. {
  47. if(grounded)
  48. {
  49. if(Input.GetButtonDown("Jump"))
  50. {
  51. rb.velocity = new Vector3(rb.velocity.x, f_JumpHeight, rb.velocity.z);
  52. }
  53. }
  54. }
  55.  
  56. void checkGround()
  57. {
  58. grounded = Physics.CheckSphere(groundChecker.position, f_GroundCheckRadius, groundMask); //! Conditions for the player to be grounded
  59. }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement