- Home /
How do I make a 2D object move in the direction it's facing, like .forward for 3D?
I hope I asked it correctly. Basically, I have a player ship, and I want it to move "forward", by pressing Up on the Keyboard, with left and right being for rotation.
When you say it "doesn't work", do you just mean that you see no movement?
If so, you'll need to deter$$anonymous$$e whether the problem is in the setup of your rigidbody (such that "rigidbody2D.AddForce()" doesn't do anything), or with your velocity calculation (such that "transform.forward boostSpeed 10" returns a zero vector, or otherwise incorrect vector)?
To decide whether it's the rigidbody or the velocity calculation try replacing line 30 ("rigidbody2D.AddForce(transform.forward boostSpeed 10)") with:
transform.Translate(transform.forward boostSpeed 10 * Time.smoothDeltaTime)
I tried this and it made the ship disappear inexplicably.
Answer by robertbu · Jan 10, 2014 at 04:39 AM
for Unity2D objects, use
transform.right
rather than
transform.forward
A 2D object moves on the XY plane. For a 2D object, transform.forward will be into the screen and therefore not have any meaning to a rigidbody2D.AddForce() call.
Depending on how you have your app setup, you will likely need either transform.up
or transform.right
or perhaps -transform.right
in your AddForce()
call.
Note that if you are using Translate
, that anyway uses local space (by default) - so it is automatic.
I thought I had done that already but it appears that I did not. I've done what you and SuperLuigi have suggested and it works correctly.
Thanks.
Answer by Mordengamer · Oct 01, 2017 at 09:44 AM
the easy way is to do transform.up instead of transform.forward its worked for me
Answer by avip805 · Sep 06, 2018 at 10:08 PM
I know this post is really old, but that has not worked for me.
-transform.up worked for me I was very close to giving up until I remembered that I forgot to try it.
I also noticed that transform.Translate(-transform.up * speed * Time.deltaTime);
didn't work But transform.position += -transform.up * speed * Time.deltaTime;
worked. Very confusing, anyone has any ideas?
Answer by superluigi · Jan 10, 2014 at 05:12 AM
In your demo when you hit right you are rotating on the Z axis clockwise in a negative direction which to my understanding means that your camera is facing forward in the Z axis. You're going to have to use
if(Input.GetKey("up"))
{
rigidbody2d.AddForce(transform.up * boostSpeed * 10);
}
I have a problem with "boostSpeed" it says it doenst exist in the current context, but i dont know how to reference it
I think that that's because you didn't have a variable called boostSpeed in your script, and you don't need to, you just need to know that you should replace boostSpeed with the speed you want the object to travel at, and you should get rid of the 10 because that's a hardcoded value. And you should add a Time.deltaTime beside the boostSpeed variable because Time.deltaTime will make it so that the object will move at the same speed even if the game was laggy
Your answer
![](https://koobas.hobune.stream/wayback/20220613125604im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
App gets stuck on WebGL 0 Answers
Check the position after drag and drop 0 Answers
Play sound once on collision 1 Answer