Make my gun point at my cursor

Im making a 2d shooter, and i want my gun to point at my cursor.
And when i point my cursor on the other side of my character, my character turns 180 degress so the gun is on ther other side of him. (Or the gun changes place)
I made a image for better explanation (my english isn’t good enough to explain this well)
As you can see on the picture, the gun is pointing against the cursor. And when i point the cursor to the other side, the gun changes place. Hope someone can show me how :)!
EDIT: Oh, and the capsule is the character, and the line in the mid is seperating the to sites.

Hi, if you get the direction you need the gun to face, you can then calculate the angle you need the gun to rotate so it points in that direction.

Traditionally, you would get the Vector between the cursor and your gun, (cursor Position - Gun position).

Then you will choose a reference axis(such as transform.right) and you would find the corressponding angle between the right(x) and direction vector by getting the Atan of the direction vector.

Then you just create a rotation matrix around the Z axis using that angle, which you can then use to transform your forward vector of your gun.

Anyway, That is the traditional way to accomplish it. Fortunately, Unity makes it really easy! :). Take a look at this function:
Unity - Scripting API: Transform.LookAt

I attached the link you send me, to my gun, but i can’t use my cursor as the target since it is a script. Is there a way to add my cursor as the target ?

What do you mean your cursor is a script? Is your cursor not a game object with a script attached to move it? How have you implemented the cursor?

have a pivot point that the gun is parented to, find the difference by subtracting the point’s vector3 from the pivot point’s vector3, i.e. crosshairVec3 - pivotPointVec3.

use either Quaternion.RotateTowards or Quaternion.Slerp to lerp from pivots current rotation to difference of the subtraction via Quaternion.LookRotation(Vector3).

The script is attached to the Camera :slight_smile:

The cursor script: var originalCursor : Texture2D;

var cursorSizeX: int = 32; // set to width of your cursor texture
var cursorSizeY: int = 32; // set to height of your cursor texture

static var showOriginal : boolean = true;

function Start(){
Screen.showCursor = false;
//Screen.lockCursor = true;
}

function OnGUI(){

if(showOriginal == true){
GUI.DrawTexture (Rect(Input.mousePosition.x-cursorSizeX/2 + cursorSizeX/2, (Screen.height-Input.mousePosition.y)-cursorSizeY/2 + cursorSizeY/2, cursorSizeX, cursorSizeY),originalCursor);
}

}

EDIT: The cursor is a texture :slight_smile:

A LookAt function should do it: Unity - Scripting API: Transform.LookAt

That is the same script i got in other comment, but i can’t attach it to my cursor because its not an object. The script is attached to the camera so that may be the ‘‘object’’. so i tried putting the script on an gameobject but the script still doesent work.

Is this top down shooter?

No its 2.5D game.

You can use Vector3 as parameters for the Transform.LookAt function you could use Input.mousePosition for the Vector3.