Advertisement
Guest User

Untitled

a guest
Apr 29th, 2017
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.19 KB | None | 0 0
  1. 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).
  2.  
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. public class PlayerMovements : MonoBehaviour
  7. {
  8.  public CharacterController player;
  9.  public float WalkSpeed = 8f, TurnSpeed = 2f, JumpForce = 4f;
  10.  public float VerticalVelocity = 0f;
  11.  public Vector3 Movements = new Vector3 (0, 0, 0);
  12.  void Awake()
  13.  {
  14.    player = GetComponent<CharacterController> ();
  15.  }
  16.  void Update ()
  17.  {
  18.    if (!player.isGrounded)
  19.   {
  20.     VerticalVelocity += Physics.gravity.y * Time.deltaTime;
  21.     VerticalVelocity = Mathf.Clamp (VerticalVelocity, -53f, JumpForce);
  22.     Movements.y = VerticalVelocity;
  23.    } else {
  24.     VerticalVelocity = 0f;
  25.     Movements.x = Input.GetAxis ("Horizontal") * WalkSpeed;
  26.     Movements.z = Input.GetAxis ("Vertical") * WalkSpeed;
  27.     if(Input.GetButtonDown ("Jump"))
  28.     {
  29.      VerticalVelocity += JumpForce;
  30.      VerticalVelocity = Mathf.Clamp (VerticalVelocity, -53f, JumpForce);
  31.      Movements.y = VerticalVelocity;
  32.     }
  33.    }
  34.    player.Move (Movements * Time.deltaTime);
  35.   }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement