- Home /
Can you change an object's forward without rotating it?
I'm working on a very simple 2D game on the XZ plane. I've created a simple cylinder which I'm using as a missile. I rotated the object so the length of the cylinder is pointed in the right direction, but the Y axis is now pointing down, so when I use transform.foward() it falls down through the background instead. I could use transform.up() and the object moves in the right direction. But when I later want to fire the missile toward a mouse-click, I can't just use transform.Lookat(), I'll have to adjust it.
Is there a way to redefine what my "forward" direction is without rotating the object?
Thanks in advance.
Answer by VesuvianPrime · Feb 08, 2015 at 02:18 AM
Changing the forward of a transform without rotating it does not make any sense. A better approach may be to parent your cylinder to another object. This way you can rotate the cylinder and use the parent for orientation.
Imagine you had a car that you were moving with transform.forward(). Now imagine that ins$$anonymous$$d of traveling in the direction of the hood, it moved laterally toward the driver side. That's my issue. I could rotate the car so that it moves in the correct direction, but it still wouldn't be moving in the direction of the hood. No matter how I rotate the object, the "forward" direction is sideways out of the driver side. I need to redefine what "forward" is on this object, which is just a cylinder in my 2D game.
What VesuvianPrime said should fix your problem. If you use an empty game object as a parent, parent the car to it, rotate the car and then use transform.forward() on the empty game object, it should move your car in the desired direction.
An even better way would be to edit your model in a 3d modelling program to ensure that it's "forward" direction was facing down the positive z axis.
Ves and Rohit: Somehow that made sense the second time I read it. The missile already has an empty parent object acting as its spawn point, and I don't want that to move. But I can include another for this job.
Tano: There's no model. This is a basic 3D cylinder in Unity.
Answer by richardgengle · Aug 02, 2020 at 05:24 AM
sprites can be flipped without rotating in inspector