Object facing the mouse pointer

I’m having a terrible time trying to find a clean and usuable way by which you move the mouse along the Y axis of the screen and an object’s Z rotation will follow the mouse pointer.

In short I want a 2D aiming system like the one in the game ‘Worms’.

Seems to have something to do with Camera.ScreenToWorldPoint but i can’t for the life of me work it out.

Thanks.

You’re on the right track. Feed what’s returned by ScreenToWorldPoint into transform.LookAt.

// Warning forum code
var distance = 1.0;
var curMousePosition;

function Update () {

    curMousePosition = Input.mousePosition;
    transform.LookAt(Camera.ScreenToWorldPoint(Vector3(curMousePosition.x, curMousePosition.y, distance)));

}

Ah great thanks.

However implimenting this code seems to give me an error.

Although I think it might be something about my editor rather than your code thats focing this error as i get similar erros quite often.

Try Camera.main. Like I said, forum code!

Ahhhhhh. Thats what that meant.

hehe.

Yes it worked perfectly.

Thank you.

Never expect any forum code to work out of the box. Consider it to be for illustration purposes only!

No problem. If you really want to thank me, click my sig and vote.

voted…

x3 apparently.