Making Cannon to look at mouse position

I have cannon and aim dot.

Aim dot should be even exactly in mouse cursor position. Cannon should look to aim.

I don't know how to have aim right under mouse cursor. I don't know how to rotate cannon to look at aim.

I have read some others question / answers about Raycast to object make looking on mouse position.

But i cannot make it works.

edit(moved from "answer")

NOw i have this, and this works, but not like i want, i need aim to be right under mouse. Don't understand how to use World coordinates of mouse, when i try my aim gets right to camera coordinates. When i try viewport coordinates ScreenToViewportPoint, my aim not under mouse too.

var cannon : GameObject;
var aimSight : GameObject;
var aim : GameObject;
var speed = 2;

function Start(){
    aim = Instantiate(aimSight, transform.position,transform.rotation);

function Update () {    
    //mp.x = Input.mousePosition;
    var mp = new Vector3();
    mp.x = Input.GetAxis("Mouse X") / speed;
    mp.y = Input.GetAxis("Mouse Y") / speed;
    mp.z = 0;
    //wmp = Camera.main.ScreenToScenePoint(mp);    
    aim.transform.position = new Vector3(aim.transform.position.x + mp.x, aim.transform.position.y + mp.y,mp.z);

Please help me (((

more ▼

asked Jun 07, 2012 at 04:55 PM

Rantiev gravatar image

0 3 2 2

I think you will get a lot of value studying the "Tower Defense Tutorials" found on Unity Cookie:


Jun 07, 2012 at 06:51 PM kolban

Thanks. I cannot understand how to use Camera.ScreenToWorldPoint because when i try camera.ScreenToWorldPoint it's give me error. I don't use attaching camera to player or another object. it seems i have bad variable camera.

Jun 07, 2012 at 11:21 PM Rantiev

@Rantiev: Don't post an answer when you don't want to answer your question. Answers should answer the question!

I've converted your answer into a comment.

Jun 07, 2012 at 11:23 PM Bunny83

@Rantiev: Ok, once more:

This is not a forum! Inform yourself how Q&A sites work and read the FAQs.

Jun 07, 2012 at 11:31 PM Bunny83
(comments are locked)
10|3000 characters needed characters left

0 answers: sort voted first
Be the first one to answer this question
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments



asked: Jun 07, 2012 at 04:55 PM

Seen: 1721 times

Last Updated: Jun 07, 2012 at 11:31 PM