Hi,
I’m trying to make a super simple topdown shooter,
I would like to make the player object look at the mouse cursor position all the time (no smooth damping or anything) but that means it needs to only rotate around its y axis or else it would be doing flips. can some one please help me with a very simple script to do this?
Thanks!
-Chris
try this guy, he shows how to control the camera to move with the mouse, it may help http://www.youtube.com/user/BurgZergArcade#p/p
I just recently helped someone else out with a problem very similar to yours. In his case, it was just the Z axis (2d side scroller), but it will be very easy to adapt it to your Y axis problem (2d top down).
http://forum.unity3d.com/threads/94267-Side-Scroller-Look-at-Cursor
Give that post a read first, so you understand the principals behind the following code snippet… But I have adapted it to work with your top-down set-up.
Do note, that the cursor position and the character position are in world space. So, you will need to turn the Input.mousePosition into world space using Camera.ScreenToWorldPoint.
// Find the difference vector pointing from the character to the cursor
Vector3 diff = cursor.transform.position - character.transform.position;
// Always normalize the difference vector before using Atan2 function
diff.Normalize();
// calculate the Y rotation angle using atan2
// Since you want Y rotation, the x component will remain the same,
// but the 'y' component will need to the the z component instead
// Atan2 will give you an angle in radians, so you
// must use Rad2Deg constant to convert it to degrees
float rotY = Mathf.Atan2(diff.z,diff.x) * Mathf.Rad2Deg;
// now assign the roation using Euler angle function
character.rotation = Quaternion.Euler(0f,rotY,0f);
Hope this is what you were looking for!