I want to be able to stop the Character Controller from continuing to move at the same rate in the air. Basically, lock it to whatever direction momentum during the jump and not allow you to suddenly go backwards from the direction you’re currently going.
using UnityEngine;
using System.Collections;
[RequireComponent (typeof(CharacterController))]
public class P1Script: MonoBehaviour {
public float nSpeed = 5; // Units per second
public float speedM = .5f; // Multiplies the "nSpeed" variable to increase movement speed
public float airSpeed = 1.5f; // Multiplies the "cAirSpeed" variable while in the air
private float cSpeed; // The current Speed
public float turnSpeed = 5.0f; // Degrees per second
public float jumpSpeed = 8; // Height of Jump
public float gravity = 9.8f; // The rate at which gravity pulls down.
private float vSpeed = 0 ; // Current Vertical Velocity
float vRotation = 0; // Prevents Pitch(?)
public float yRange = 60.0f; // The angle limit for the Y-Axis look
CharacterController cController;
// Use this for initialization
void Start () {
Screen.lockCursor = true; // Locks Cursor to Screen; makes invisible
cController = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update () {
//Jump
if(cController.isGrounded) {
vSpeed = 0; // Grounded character has vSpeed = 0...
if(Input.GetButton("Jump")) {
vSpeed = jumpSpeed;
var cAirSpeed = cSpeed / 2;
cSpeed = cAirSpeed;
}
}
// Rotation and Walk
float rotLeftRight = Input.GetAxis("Mouse X");
transform.Rotate(0, rotLeftRight, 0);
var vel = transform.forward * Input.GetAxis("Vertical") * cSpeed;
vRotation -= Input.GetAxis("Mouse Y");
vRotation = Mathf.Clamp(vRotation, -yRange, yRange);
Camera.main.transform.localRotation = Quaternion.Euler(vRotation, 0, 0);
var hor = transform.right * Input.GetAxis("Horizontal") * cSpeed;
vSpeed -= gravity * Time.deltaTime; // Applies gravity acceleration to the vertical speed
vel.y = vSpeed; // include vertical speed in the vel
cController.Move (vel * Time.deltaTime); // Convert vel to displacement and move the character
hor.y = vSpeed; // Applies gravity acceleration to the horizontal speed:
cController.Move (hor * Time.deltaTime); // Convert hor to displacement and move the character
//Sprinting
if (Input.GetButton("Sprint")) { // Check for "Sprint" as defined by project inputs
cSpeed = nSpeed * speedM;
} else {
cSpeed = nSpeed;
}
}
}