If your object doesn’t already “look down” the Z vector (Z+), one handy way to change that is to insert an extra gameobject in the hierarchy and then pivot that new gameobject, such that the objects below it rotate the way you want to face the parent objects Z+ look direction.
For instance, if you wanted to “point” the default Unity Capsule object lengthwise (by default it points Y+ or Y-), then you could parent it to a blank gameobject, rotate the capsule’s local position by (90,0,0), and then actually aim the blank gameobject above it in the hierarchy. The child capsule will now be aligned “towards” the target lengthwise.
That way you can continue to benefit from the Unity3D math libraries without rolling your own. It can be a bit tricky to get the hang of parenting things correctly like that, but once you get it, you’ll see how much easier and cleaner it can be.
Use Unity’s Vector2.Angle(a, b). Get the normal vector from the point to the weapon and then check the angle between that and and the Vector2.up. As it returns angles between [0, 180] then make sure to do a 360 - angle if needed (based on the normal vectors direction). Then rotate the weapons transforms rotation (eulerangles, not rotation) in the z axis with the angle.
Thank you all for replying.
To Kurt, I’m not too sure what you mean. Do you think I should make all my objects point in the Z direction as default and then rotate them for 2D purposes?
I made version of my project yesterday where instead of using XY plane, I used ZY plane rather, but that doesn’t really seem like a good solution as I do not know if that’d become a problem since that pressing the 2D button always places me in an XY view.
and BFGames! Thank you! I read your suggestion but I’m not too sure if I implemented it correctly. Here’s a screen of the result of my code. I printed the values of the eulerangles.z and the angle calculated and the weird thing is even though it’s not pointing to it, they both print out the same number.
Vector2 normal = (point.position - transform.position).normalized;
float angle = Vector2.Angle(normal,Vector2.up);
Vector3 curRot = transform.eulerAngles;
curRot.z = angle;
transform.eulerAngles = curRot;
Debug.Log("Angle: " + angle);
Debug.Log("cur: "+ transform.eulerAngles.z);
Let me add a tiny bit of (text mode) graphics to my above statement.
Here is how you would put an extra object into the scene between the helicopter and the gun, such that the hierarchy would look like so:
Old way: (this is not code, this is lines in the hierarchy window)
Helicopter
TheGun (points the wrong way for your "look at")
New way:
Helicopter
GameObject (let's call this Pivot)
TheGun
Then you would select TheGun and adjust its rotation, and pivot it around so that it aims down the Z+ axis of its parent, the Pivot.
Your aiming function would no longer adjust the gun itself but rather adjust the Pivot, causing the child object (the gun) to aim correctly.
Oh I see what you mean Kurt Dekker, I thought of doing that before which means I could use LookRotation. I turned the gun to face in the direction of its parent positive Z and then I rotated the parent 90 on the Y and it looks exactly as it did before and I used lookrotation script on parent and it seems fine. Is that really good practice for 2D? I guess whatever works is good practice.
The issue I had with that is that I then do not know how to make it rotate with a set speed. I do not understand the slerp or lerp functions last parameter t. I’ve looked it up multiple times and I’m still confused and the example documentation doesn’t really help. The other thing that, perhaps I was wrong to deter from it for is limitations on rotation like if i want the object to only be able to go between 90 and 270 degrees. I’m sorry if I seem like a confused noobie, rotations just confuse me.