- Home /
Isuues with Gravity in Unity 5
So far, I have a simple First Person Shooter going. As our player looks up, they speeds forward. As the player looks down, they hustle backwards.
Here is the script to handle character movement:
float forwardSpeed = Input.GetAxis ("Vertical") * PlayerSpeed;
float sideSpeed = Input.GetAxis ("Horizontal") * PlayerSpeed;
verticalVelocity += Physics.gravity.y * Time.deltaTime;
if( characterController.isGrounded && Input.GetButton("Jump") )
{
verticalVelocity = jumpSpeed;
}
Vector3 move = new Vector3 (sideSpeed, verticalVelocity, forwardSpeed);
move = transform.rotation * move;
characterController.Move (move * Time.deltaTime);
Taking a deep look into the problem, I'm thinking that the engine is just applying gravity to the main camera's down, and not a true down (This problem is no longer present when the gravity lines of code are noted out, and the velocity at which the player speeds is increasing). So, if the camera rotates up, the gravity's vector does too. How would I make it so that gravity is applied to the player's capsule collider, instead of the camera?
Answer by nisovin · Jul 23, 2015 at 05:34 PM
You're multiplying the movement vector by the character's rotation, which includes all three axes of rotation. This is why you're experiencing the weird movement. You aren't even using the physics engine to apply gravity here, you're just adding it in yourself, so the physics engine doesn't have anything to do with it.
Your answer
Follow this Question
Related Questions
How can I rotate an object to be perpendicular to a surface it hits (in 2D); 1 Answer
How to rotate object in instantiate with float value 1 Answer
Question on 2d planet gravity 1 Answer
Slerp Problem?: Enemy in Constant Rotation! 1 Answer
Rotate on one axis and check if angle is inside desired range 2 Answers