x


Mouse Camera, Camera Mouse

Right. I have tried asking many many people how to do this script and no one seems to get what it is I mean. Basically I want this to be a valid script

function Update () {        
transform.position = Vector3(Input.mousePosition);
}

Which frustratingly it isn't. I want the mainCamera x and y co-ordinates to equal the x and y coordinates of the mouse cursor. It should be simple but it has given me a lot of difficulty.

I do have a script that almost works but I don't think I'm going in the right direction so I'm not going to put it up because it just confuses people usually.

more ▼

asked Jan 12 '10 at 12:27 AM

Sam Beckham gravatar image

Sam Beckham
55 3 3 14

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Input.mousePosition is a Vector3 already....

Are you working in C# or javascript? You tagged the question with c#

in C# (C# scripts must be named the same as their class name, in this example CamMouse)

using UnityEngine;
using System.Collections;

public class CamMouse : MonoBehaviour {


    void Update () {
        transform.position = Input.mousePosition;
    }
}

in Javascript

function Update () {
	transform.position = Input.mousePosition;
}

or, for fun and syntax similar to yours, in C#

using UnityEngine;
using System.Collections;

public class CamMouse : MonoBehaviour {


    void Update () {
        transform.position = new Vector3(Input.mousePosition.x,
             Input.mousePosition.y,
             Input.mousePosition.z);
    }
}

Also, note that Input.mousePosition goes between (0,0,0) in the lower left corner, to (Screen.Width, Screen.Height, 0) in the upper right.

more ▼

answered Jan 12 '10 at 12:55 AM

Brian Kehrer gravatar image

Brian Kehrer
2.8k 9 11 50

Dude I love you, been stuck on this for ages!!

I didn't mean to tag it as c#, it was an old script I was working on that was in c# but I'm glad I did because this works perfectly :D. I had a very similar script before but I missed out the word new and it didn't work. Rather annoying how 3 letters can be the difference between something working perfectly and it doing nothing at all.

Jan 13 '10 at 01:24 PM Sam Beckham
(comments are locked)
10|3000 characters needed characters left
Your answer
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

By RSS:

Answers

Answers and Comments

Topics:

x5099
x3015
x986

asked: Jan 12 '10 at 12:27 AM

Seen: 8612 times

Last Updated: Jan 12 '10 at 12:36 AM