Just for the sake of experimenting I’m trying to make a pretty simple side scroller game system. I have a pretty basic player that walks forwards and backwards and jumps and a cursor, which is an actual game object, positioned in space using the ScreenToWorldPoint from the camera class and the Z distance between the camera and the player.
Now since the cursor is a gameObject I thought that it would be easy to make a weapon which is attatched to the player and rotates to face the cursor but it’s not so easy because I need to rotate the weapon on the Z axis only so the X axis would point exactly at the cursor.
I tried different methods like:
transform.rotation.z = Vector3.Angle(transform.position, aim.transform.position);
and
transform.rotation = Quaternion.AngleAxis(Vector3.Angle(transform.position, aim.transform.position), Vector3.forward);
None of them works correctly. How do I achieve this? I feel like I’m close to solving this but I can’t.
It’s actually easier to use the Z axis as the weapon’s forward direction because then you can use transform.LookAt to make it point toward the cursor object:-
transform.LookAt(aim.transform);
Yes, but I decided to use the X and Y axes because in 2D there’s only X and Y. I already made other things using the X and Y axes and I’ll have to redo everything and I’m also interested in how that type of rotation is done. I was thinking if I could use WorldToScreenPoint to get the position of the weapon and the aim object on the screen and then somehow calculate an angle but I don’t know how to calculate the angle and I’m not sure if it’s really going to work. Do you happen to have any other ideas rather then LookAt? 
If I understand correctly, you are looking for a way of rotating around just the Z axis of the weapon so the positive X axis points towards the cursor’s world position. I use the Atan2 approach to this problem, which will take the x and y components of a difference vector pointing from your weapon’s position to the cursor’s position and convert it into a rotation.
A few important notes are…
- Your weapon’s business end(barrel) must point in the positive X direction for this to work. If not, you will have to offset the resulting angle in order for it to point in the correct direction.
- The weapon is going to rotate around its origin, so if you want the weapon to rotate from a certain point, be sure that point is the origin.
// Find the difference vector pointing from the weapon to the cursor
Vector3 diff = cursor.transform.position - weapon.transform.position;
// Always normalize the difference vector before using Atan2 function
diff.Normalize();
// calculate the Z rotation angle using atan2
// Atan2 will give you an angle in radians, so you
// must use Rad2Deg constant to convert it to degrees
float rotZ = Mathf.Atan2(diff.y,diff.x) * Mathf.Rad2Deg;
// now assign the roation using Euler angle function
weapon.rotation = Quaternion.Euler(0f,0f,rotZ);
Great! That’s what I needed. It works exactly as I wanted. Thank you.
I would reallllly like to implement this into my code!
Only I do not understand C# syntax very well if at all.
But Unity is telling me that cusor and weapon are not recognized in the context. what steps do I need to take to get this to work on my script???