Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- The following code does not have any problems jumping using isGrounded. It also clamps vertical velocity to the maximum free fall velocity of 120 mph (53 meters/second).
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class PlayerMovements : MonoBehaviour
- {
- public CharacterController player;
- public float WalkSpeed = 8f, TurnSpeed = 2f, JumpForce = 4f;
- public float VerticalVelocity = 0f;
- public Vector3 Movements = new Vector3 (0, 0, 0);
- void Awake()
- {
- player = GetComponent<CharacterController> ();
- }
- void Update ()
- {
- if (!player.isGrounded)
- {
- VerticalVelocity += Physics.gravity.y * Time.deltaTime;
- VerticalVelocity = Mathf.Clamp (VerticalVelocity, -53f, JumpForce);
- Movements.y = VerticalVelocity;
- } else {
- VerticalVelocity = 0f;
- Movements.x = Input.GetAxis ("Horizontal") * WalkSpeed;
- Movements.z = Input.GetAxis ("Vertical") * WalkSpeed;
- if(Input.GetButtonDown ("Jump"))
- {
- VerticalVelocity += JumpForce;
- VerticalVelocity = Mathf.Clamp (VerticalVelocity, -53f, JumpForce);
- Movements.y = VerticalVelocity;
- }
- }
- player.Move (Movements * Time.deltaTime);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement