Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class playerMovement : MonoBehaviour
- {
- public float f_MoveSpeed = 5f; //! Defines the move speed for the player
- public float f_JumpHeight = 2f; //! Defines the jump height for the player
- private Vector3 vec_Input; //! A vector 3 to store our input
- private Rigidbody rb; //! Rigidbody variable stored on our player
- public Transform groundChecker; //! A transform which will collide with the ground
- public float f_GroundCheckRadius = 0.4f; //! The size of the sphere checking if the player is on the ground
- public LayerMask groundMask; //! The mask used for the ground
- [SerializeField]private bool grounded; //! A bool to store if the player is grounded or not
- public VariableJoystick joyStick; //! A variable for the joystick
- private void Start()
- {
- rb = GetComponent<Rigidbody>();
- }
- void FixedUpdate()
- {
- movement();
- jump();
- }
- private void Update()
- {
- checkGround();
- }
- void movement()
- {
- #if UNITY_STANDALONE_WIN || UNITY_EDITOR
- vec_Input = new Vector3(Input.GetAxis("Horizontal") * f_MoveSpeed, rb.velocity.y, Input.GetAxis("Vertical") * f_MoveSpeed);
- rb.velocity = transform.TransformDirection(vec_Input);
- #endif
- #if UNITY_ANDROID
- vec_Input = new Vector3(joyStick.Horizontal * f_MoveSpeed, rb.velocity.y, joyStick.Vertical * f_MoveSpeed);
- rb.velocity = transform.TransformDirection(vec_Input);
- #endif
- }
- void jump()
- {
- if(grounded)
- {
- if(Input.GetButtonDown("Jump"))
- {
- rb.velocity = new Vector3(rb.velocity.x, f_JumpHeight, rb.velocity.z);
- }
- }
- }
- void checkGround()
- {
- grounded = Physics.CheckSphere(groundChecker.position, f_GroundCheckRadius, groundMask); //! Conditions for the player to be grounded
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement