- Home /
Rigidbody slowly moves
My player in my game has a rigidbody attatched to him. I use the AddForce function to move him around. I also have a terrain set up. My drag variable of the rigidbody is set to 4 and it has a mass of 1. Gravity is true. The Gravity var moved my player to slowly so I also added a constant force with a -40y force. Everything works great but when I look down or up the player slowly creeps forward when looking down or backwards when looking up. I look around with the mouselook script from the character controllers package. I was wondering is there a way to stop this from happening. The player has a capsule collider also.
This is the code that makes my Player move
var water : boolean;
var water1 : boolean;
var cam : Transform;
var water2 : boolean;
function Start () {
}
function Update () {
if(Input.Get$$anonymous$$ey("w") && Input.Get$$anonymous$$ey($$anonymous$$eyCode.LeftShift) && !water1){
rigidbody.AddForce(new Vector3(transform.forward.x, 0 , transform.forward.z) * 50);
}
if(Input.Get$$anonymous$$ey("w") && !Input.Get$$anonymous$$ey($$anonymous$$eyCode.LeftShift)&& !water1){
rigidbody.AddForce(new Vector3(transform.forward.x, 0 , transform.forward.z) * 40);
}
if(Input.Get$$anonymous$$eyDown("space") && !Grounded && !water2){
rigidbody.AddForce(Vector3.up * 1300);
}
if(Input.Get$$anonymous$$ey("a")&& !water1){
rigidbody.AddForce(transform.right * -30);
}
if(Input.Get$$anonymous$$ey("d")&& !water1){
rigidbody.AddForce(transform.right * 30);
}
if(Input.Get$$anonymous$$ey("s")&& !water1){
rigidbody.AddForce(transform.forward * -30);
}
//water movement
if(Input.Get$$anonymous$$ey("w") && Input.Get$$anonymous$$ey($$anonymous$$eyCode.LeftShift) && water1){
rigidbody.AddForce(new Vector3(transform.forward.x, 0 , transform.forward.z) * 30);
}
if(Input.Get$$anonymous$$ey("w") && !Input.Get$$anonymous$$ey($$anonymous$$eyCode.LeftShift)&& water1){
rigidbody.AddForce(new Vector3(transform.forward.x, 0 , transform.forward.z) * 20);
}
if(Input.Get$$anonymous$$ey("space") && water2){
rigidbody.AddForce(Vector3.up * 60);
}
if(Input.Get$$anonymous$$ey("a")&& water1){
rigidbody.AddForce(transform.right * -20);
}
if(Input.Get$$anonymous$$ey("d")&& water1){
rigidbody.AddForce(transform.right * 20);
}
if(Input.Get$$anonymous$$ey("s")&& water1){
rigidbody.AddForce(transform.forward * -20);
}
//Water
if(cam.transform.position.y < 0){
water = true;
}
if(cam.transform.position.y > 0){
water = false;
}
if(transform.position.y <= 0){
water1 = true;
}
if(transform.position.y >= 0){
water1 = false;
}
if(transform.position.y >= -.5){
water2 = false;
}
if(transform.position.y <= -.5){
water2 = true;
}
}
function OnCollisionEnter(other : Collision){
Grounded = false;
if(other.gameObject.tag == "Raft"){
transform.position.y = 1;
}
}
function OnCollisionExit(other : Collision){
Grounded = true;
}
I have found out it has something to do with the collider. I change it to a sphere collider ins$$anonymous$$d of a capsule collider and it does it less. It stops doing it on flat terrain but still happens on a slope.
Answer by Baste · Mar 11, 2015 at 11:12 PM
Don't add force in the direction of transform.forward. If you're looking straight down, transform.forward.x will be very close to 0, so doing this:
rigidbody.AddForce(new Vector3(transform.forward.x, 0 , transform.forward.z) * 50);
will give you a much lesser speed when looking down than when looking straight ahead.
The easiest thing to do is to remove the y-component from your forward-vector, and then normalize it. That will give you a vector that has constant length, no matter what the tilt of your look is:
Vector3 myForward = transform.forward;
myForward.y = 0f;
myForward = myForward.normalized;
if(Input.GetKey("w") && Input.GetKey(KeyCode.LeftShift) && !water1){
rigidbody.AddForce(new Vector3(myForward.x, 0 , myForward.z) * 50);
}
Do the same thing for transform.right.
I'd also suggest trying to reduce the immense amount of if/elses. Instead of checking left shift and water in every one of those, create a speed variable based on shift and water, and use that for what you multiply your speed with.
Your answer
Follow this Question
Related Questions
OnCollisionEnter not entered on obvious collision 2 Answers
Character controller with Rigidbody to apply force 0 Answers
rigidbody to my player and a box collider to my background not working 0 Answers
Making a surface have accurate friction 0 Answers
My Player With a Capsule Collider is Tipping Over and Rolling After I Added a Rigidbody 1 Answer