2d Mouse Aiming

**[SOLVED]**Hey guys, I’m creating a 2D sidescroller game and I came across a problem I can’t resolve, I’ve Spent 2 Days googling “2d mouse aiming” and others but I can’t find any luck, can someone at least give a suggestion or a script that allows “2d mouse aiming” (an object that actually looks at the mouse cursor and rotates the object on its z axis) on a Sprite or Gameobject in 2d space?

any suggestions please!?!?!

Just a suggestion/Idea - not sure though.

Personally I would look at making a gameObject constant translate to my mouse location.

There for (just guessing here) I would have to do my code in either Fixedupdate or Lateupdate.
Enough with that…

First i would get the mouse location using Input.mousePosition

Then store it in as a vector so i can translate the gameobject to that location.
The exsample in the link is nice, it’s allready showing a raycast aswell, if you where to shoot or click on something.

Now that my gameObject is always under my mouse
I would then look into turning my sprite? or model accordingly towards the mouse.

I cannot give you a actual script exsample. but i think i saw a demo game in the asset store with this.
It was either in Android game bundles or 25 games for 20$ package, cant remember one of those had the thing your describing there.

I hope it helps.

EDIT!! Go download the angrybots DEMO in the assets store BY UNITY!
web demo here - Discover the latest Unity demos and projects | Unity

IT has a topdown controller, with always look to mouse, in 3D.
But with that it should be possible to convert it to a 2D solution!

Ok, Heres my code, It won’t work but I trying to find a way how to store the mouse input as a vector 3 or 2 (don’t know whats the difference between the two). I’m using a different approach so instead of making the object rotate to the cursor i will actually create a custom cursor (a game object in the scene) make it stick to the mouse actual mouse position and than make the object rotate to the custom cursor using “transform.LookAt()” and than 0 out the x and y rotation.

#pragma strict
 
var cursor : Transform;

function FixedUpdate () {
	var mousePos = Input.mousePosition;
	cursor.position = mousePos;
}

EDIT: I have checked out the angry bots demo and looked at the cursor code, not sure how to convert the code or rip it out.

ok I got the cursor script to work but how can i make it move like an actual cursor script, apparently it is moving way off screen to where the camera cannot even see it. how can i make it move WITHIN the view of the camera/game view or screen?

#pragma strict

var mousePos : Vector3;
var cursor : Transform;

function FixedUpdate () {
	mousePos = Input.mousePosition;
	cursor.position = (mousePos);
}

any Suggestions or Answers?!?!!?!?!!?!? Please :sad:

Nvm I fixed it

[This Thread is solved] BTW if anyone needs 2d mouse aiming you can use my script i have here, This is for a custom cursor (using a quad or sprite)

public var distanceFromCamera = 10.0;
 
function Update() {
    var pos = Vector3(Input.mousePosition.x, Input.mousePosition.y, distanceFromCamera);
    pos = Camera.main.ScreenToWorldPoint(pos);
    transform.position = pos;
}

and for the player to look at the cursor

#pragma strict

var cursor : Transform;

function Update () {
	transform.LookAt(cursor);
	transform.rotation.x = 0;
	transform.rotation.y = 0;
}
1 Like

that part about transform.rotation.x = 0 doesn’t work in unity 5. How do I adapt it?