Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class PlayerMovement : MonoBehaviour {
- public float speed;
- float jumpHeight = 8f;
- //Players Movement
- public Vector3 direction = Vector3.zero;
- float verticalVeolocity = 0;
- //End Player Movement
- //Components
- CharacterController cc;
- //End Components
- void Awake() {
- speed = 8;
- }
- void Start () {
- //Components
- cc = GetComponent<CharacterController> ();
- }
- void Update () {
- //The players direction using AD, left arrow and right arrow.arrow.
- direction = transform.rotation * new Vector3 (Input.GetAxisRaw ("Horizontal"), 0, 0);
- //If the Character Controller is grounded then allow the player to Jump.
- if(cc.isGrounded && Input.GetButton("Jump")) {
- //WQhen ever the player jumps we want to set his verticalVeolocity to jumpHeight.
- verticalVeolocity = jumpHeight;
- }
- }
- void FixedUpdate() {
- Vector3 dist = direction * speed * Time.deltaTime;
- //Jumping
- if (cc.isGrounded && verticalVeolocity < 0) {
- verticalVeolocity = Physics.gravity.y * Time.deltaTime;
- } else {
- if(Mathf.Abs (verticalVeolocity) > jumpHeight * 0.75) {
- //Put animation for jumping here!
- }
- //Apply Gravity
- verticalVeolocity += Physics.gravity.y * Time.deltaTime;
- }
- //Add the verticalVeolocity to our actual movement this frame.
- dist.y = verticalVeolocity * Time.deltaTime;
- //Apply the movement to the CC.
- cc.Move (dist);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement