- Home /
Movement with AddForce: Wrong Direction
Hey everyone; I have a Player Controller script that is supposed to apply a force to the player GameObject to make it move in a direction based upon user input.
public void MoveUpdate () {
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
if(h * this.rigidbody.velocity.x < maxSpeed) {
this.rigidbody.AddForce(Vector3.right * h * moveForce);
}
if(Mathf.Abs(this.rigidbody.velocity.x) > maxSpeed) {
this.rigidbody.velocity = new Vector3(Mathf.Sign(this.rigidbody.velocity.x) * maxSpeed, this.rigidbody.velocity.y, this.rigidbody.velocity.z);
}
if(v * this.rigidbody.velocity.z < maxSpeed) {
this.rigidbody.AddForce(Vector3.forward * v * moveForce);
}
if(Mathf.Abs(this.rigidbody.velocity.z) > maxSpeed) {
this.rigidbody.velocity = new Vector3(this.rigidbody.velocity.x, this.rigidbody.velocity.y, Mathf.Sign(this.rigidbody.velocity.z) * maxSpeed);
}
if(h > 0 && !facingRight) {
Flip();
}
else if(h < 0 && facingRight) {
Flip();
}
}
When I press "w" or "up," the forward input buttons (a.k.a. Input.GetAxis("Vertical")), the player also moves a little bit to the left or right, though. Why might this be happening? I would like the player to only move forward if only the "w" or "up" inputs are activated.
Edit: I've verified that the "h" value is never anything higher or lower than zero when this happens, and I've tested this script on a basic cube object, with the same results.
Edit #2: I've discovered that the issue is actually because I am using more than one collider to create a compound collider; when turning off one, it works perfectly. What is the best way to get around this? I need multiple colliders to match the shape of my character correctly.
Start by doing:
Debug.Log(h+", "+v);
If you are seeing any non-zero value of 'h' when you only press a vertical key, then you may want to adjust the dead zone for the 'Horizontal' axis: Edit > Project Settings > Input. Edit the 'Dead' setting.
Hey robertbu; I am always seeing zero for h when pressing the vertical key.
Answer by agies1 · Jan 12, 2014 at 07:43 PM
Your issue is more than likely due to physics drag. Try going to your projects Physics settings and switch the material to a super smooth Physics material (you can pull it out of the Physics material Asset Package). If your odd movement is fixed, you should consider using a Capsule or Sphere collision box (they have less surface area and will create less drag), instead of a cube, or use the default smooth physics material.
Thanks for the suggestion agies1, I'll try that out tonight. I just realized that I'm also using two sphere colliders as "feet" for my game's character, and that they overlap; that might be part of the problem (Though not all of it, as I've had the same issue with simple shapes), and I may need to set them so that they ignore collisions between eachother. I'll play around with the physics materials and get back to this thread with my results.
I've discovered that the issue is actually because I am using more than one collider to create a compound collider; when turning off one, it works perfectly. What is the best way to get around this? I need multiple colliders to match the shape of my character correctly.
Use a mesh colided they are more expensive on a object but if just on the player should be no problem. Also a capsule collider is good but you have to be ok with it not being a perfect fit Also double check your followers are facing the same direction you could be pushing against each other
$$anonymous$$y character is a sprite drawn at an angled perspective, so that wouldn't work. I can try using a single collider, such as a sphere or capsule collider, but the character's shape is still somewhat complex; is there any way at all to get this to work with compound colliders?
Did you double check your colliders to make sure they are all facing the same direction
Answer by Grim_Darknight · Jan 20, 2014 at 09:21 AM
The best way that I fould to have compond colliders on objects withou causing Physics issues is to set their collision properties so that they are unable to collide with themselves. The biggest issue with this will be that clones of objects setup like this wont collide with each other.
I've tried doing that before, but that didn't seem to work.