I'm trying to make a 2D shooter where the gun follows the mouse and fires in the direction you are aiming. I'm trying to get the gun to rotate to follow the mouse right now and I've tried Code (csharp): Vector2 MS = Input.mousePosition; float Compare = Vector2.Angle(new Vector2(transform.position.x, transform.position.y), new Vector2(MS.x, MS.y)); Vector3 Rot = new Vector3(0,0, Compare); transform.LookAt(Rot); But the gun isn't rotating at all, what am I doing wrong?
Input.mousePosition has no meaning in world space, so your compare is likewise meaningless read / use this http://docs.unity3d.com/Documentation/ScriptReference/Camera.ScreenToWorldPoint.html
Okay so I set my mouse position to world coords Code (csharp): MS = Camera.mainCamera.ScreenToWorldPoint(Input.mousePosition); And switched Compare to Vector3 Values Code (csharp): Compare = Vector3.Angle(transform.position, MS); But still the Gun does not rotate at all, I've set several of the values to public and watched them and they work, it seems like LookAt just isn't doing anything.
I changed up the LookAt to: Code (csharp): transform.LookAt(new Vector3(MS.x, MS.y, 0)); except now that it's rotating on the wrong axis (X) when I want it to rotate on the Z axis. I've tried switching around where MS.x and MS.y are but I can't get the cube to rotate right.
That makes it spin on the Y axis with some spin in the X. Quick second question how do I move the pivot? It's a cube I'm using with a material slapped on, I don't want it to rotate around the center.
Okay so I rotated the cube 90 degrees so that an X-Axis rotation provides the effect I need, however I still have the pivot problem. Is there a way to offset the pivot of a basic cube in Unity?
No exactly What you could do is put the cube as a child of an empty GO, with a different position, then apply the script to that instead create -> empty GO drag cube onto GO in the hierarchy
Why is the Y rotation switching between 90 and 270? It works all the way up till it flips upside down. I even added a rigid body and restricted the Y rotation, but that did nothing.
here is a tutorial I found for a top down shooter if it helps. http://www.rebelplanetcreations.com/downloads/Other/Tutorials/HowToMakeAGameInUnity3D.pdf