- Home /
How do I use Vector3 with rigidbody.Addforce
I am not quite sure how to phrase the question but I tried my best. Alright, I have my function to go forward, bla bla bla. The real problem I am getting from this script is the inability to move my character the direction it is pointed to. Here is my line Distance is a variable btw, rigidbody.Addforce(Vector3.forward*distance*Time.deltaTime)
I have tried this aswell.. rigidbody.Addforce(Vector3(0,0,distance)
and this does work except it only moves in the position that was set in the beginning, or in other words, when I rotate it, it doesn't go in the direction it is facing, any ideas on how I could, move it in the direction it is facing.
I'm a complete newbie with JavaScript and I would like to know, so I can finally get my character moving properly
Thanks in advanced :D.
Answer by Berenger · Feb 01, 2014 at 11:05 AM
Problem is Vector3 is constant of value (0, 0, 1), meaning it doesn't change, ever. That makes you move along the Z axis only. Same with Vector3(0,0,distance).
Use transform.forward instead. You'll still need a way to rotate though.
Answer by gfoot · Feb 01, 2014 at 11:12 AM
You should use a multiple of transform.forward, which is your object's forward vector.
You shouldn't use Time.deltaTime with forces - they are applied continuously over the course of the frame, so Unity does this multiplication for you under the hood. You also shouldn't use a distance as the magnitude of the force - they're different physical quantities. To begin with just try this:
rigidbody.AddForce(transform.forward)
and multiply it by something if you want it to move faster/slower.
Thanks for the great advice (Thanks to Berenger aswell) but now I am plagued with another question XD... I tried to put "left" ins$$anonymous$$d of forward for the transform.forward part, and it seems to pause the game as if something was wrong, and I would like to know how that works? I tried making it a variable and adding 180 to it (for the backwards movement) but apparently it doesn't like that syntax either. What could I do?
Oh never $$anonymous$$d, transform.right exists :D funny scripts, could you tell me how to use "transform.up" for jumping ins$$anonymous$$d, because that doesn't work and how to limit the Velocity ( I am not sure how to use normalization).. Thanks!
Your answer
Follow this Question
Related Questions
Can't move player with rigidbody.velocity 1 Answer
Change player movement 0 Answers
Npc Movement 0 Answers
Constrain character to series of intersecting paths? 1 Answer