- Home /
Prevent rigidbody.AddForce from moving character on global Y axis
I have a rigidbody first person character. To move, you click and hold the mouse and rigidbody.AddForce will accelerate character in that direction. This mostly works fine, but when the player looks up and clicks, they are able to fly.
I want to prevent rigidbody.AddForce from applying any force to the global Y axis. Freezing the position on the Y axis on the rigidbody is almost exactly what I want, but it means the level must be completely flat and the character wont be able to move up or down slopes.
Is there a way to only apply force to the X and Z axis? Here's what I'm currently using:
function FixedUpdate () {
if (Input.GetMouseButton (0))
{
rigidbody.AddForce(-transform.forward * 50);
}
}
Hello! I'm a newbie. I've got a problem when using rigidbody.AddForce to a humanoid character. Here is my script:
function Update () {
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.C))
{
rigidbody.AddForce(transform.forward*5,Force$$anonymous$$ode.VelocityChange);
}
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.V))
{
rigidbody.AddForce(transform.up*5,Force$$anonymous$$ode.VelocityChange);
}
}
I've tried it with a Cube and it still work well. But when I use it to my character (rig : humanoid) it does not work. I realize when I remove Animator Controller of my character, it work like a Cube. Thanks for watching!
Technically you should post another question - I have converted your answer (which this is not) to a comment.
Various CharacterController based components are faux physics like rigid bodies, but they do not have as rigid bodies, so likely you have a RigidBody and a CharacterController on the same GameObject and they are not working well together.
Answer by Bovine · Jul 14, 2013 at 09:35 PM
Something like:
Vector3 forward = transform.forward;
forward.y = 0;
forward.Normalise();
This is pseudocode, so you'll need to check. Naturally then apply force to the RigidBody as you are, replacing -transform.forward with your forward vector.
Unless I'm missing something here?
Yeah, this is exactly what I want. I've been making things much more complicated than necessary. Thanks for the fast answer.
Your answer
Follow this Question
Related Questions
Simple rigidbody.AddForce only applying once each time key is pressed 1 Answer
Is Add Force The Best Solution 1 Answer
Move object invertically to another 0 Answers
Have player fall down with platform? 0 Answers
Smooth dashing issues 0 Answers