- Home /
Problem with moving my player whilst in the air
Hello, I have got a script which check the player's input on the horizontal axis and vertical axis and then translates it into a vector so the character controller can move and rotate into that direction. However I have an issue because when I try to move the player while it's in the air, it won't move but it will rotate. For example if I was in the air and held down the forward key, the player would turn tot look forward but won't actually move forward. The ground controls and fine but it would be better if I could move the player while also in the air. Any help would be appreciated a lot. Here is my script so far:
var autoRotate : boolean = true;
var maxRotationSpeed : float = 360;
var hoirzontalAxis = "Horizontal";
var verticalAxis = "Vertical";
var jumpButton = "Jump";
var myCam : Camera;
var on = true;
var camFollow : Transform;
var moveSpeed : float = 8.0;
var runningSpeed : float = 16.0;
var playerAudioSource : AudioSource;
private var controller : CharacterController;
function Awake () {
controller = transform.GetComponent(CharacterController);
}
function Update () {
var directionVector : Vector3;
var keyLook : CameraController;
keyLook = myCam.GetComponent(CameraController);
var isKeyLook = keyLook.keyLook;
if(isKeyLook)
directionVector = new Vector3(0, Input.GetAxis(verticalAxis), 0);
if(!isKeyLook)
directionVector = new Vector3(Input.GetAxis(hoirzontalAxis), Input.GetAxis(verticalAxis), 0);
if (directionVector != Vector3.zero) {
// Get the length of the directon vector and then normalize it
// Dividing by the length is cheaper than normalizing when we already have the length anyway
var directionLength = directionVector.magnitude;
directionVector = directionVector / directionLength;
// Make sure the length is no bigger than 1
directionLength = Mathf.Min(1, directionLength);
// Make the input vector more sensitive towards the extremes and less sensitive in the middle
// This makes it easier to control slow speeds when using analog sticks
directionLength = directionLength * directionLength;
// Multiply the normalized direction vector by the modified length
directionVector = directionVector * directionLength;
}
// Rotate the input vector into camera space so up is camera's up and right is camera's right
directionVector = myCam.transform.rotation * directionVector;
if(Input.GetKey(KeyCode.LeftShift)){
moveSpeed = runningSpeed;
}else{
moveSpeed = 8.0;
}
// Rotate input vector to be perpendicular to character's up vector
var camToCharacterSpace = Quaternion.FromToRotation(-myCam.transform.forward, transform.up);
directionVector = (camToCharacterSpace * directionVector);
controller.SimpleMove(directionVector * moveSpeed);
// Set rotation to the move direction
if (autoRotate && directionVector.sqrMagnitude > 0.01) {
var newForward : Vector3 = ConstantSlerp(
transform.forward,
directionVector,
maxRotationSpeed * Time.deltaTime
);
newForward = ProjectOntoPlane(newForward, transform.up);
transform.rotation = Quaternion.LookRotation(newForward, transform.up);
}
}
function ProjectOntoPlane (v : Vector3, normal : Vector3) {
return v - Vector3.Project(v, normal);
}
function ConstantSlerp (from : Vector3, to : Vector3, angle : float) {
var value : float = Mathf.Min(1, angle / Vector3.Angle(from, to));
return Vector3.Slerp(from, to, value);
}
Answer by FortisVenaliter · May 17, 2015 at 06:42 PM
I'm not 100% sure from documentation, but my guess is that CharacterController.SimpleMove will only move while grounded. In the docs, it says it returns a bool of whether or not it's grounded, so I think that may be your problem. You'll have to find an alternate method of moving the character than that function.
Thanks for the help it is much appreciated. I changed the Simple$$anonymous$$ove to $$anonymous$$ove and it works now! Thanks! :)