- Home /
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.
Answer by Brian-Kehrer · Jan 12, 2010 at 12:55 AM
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.
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220611225835im_/https://answers.unity.com/themes/thub/images/avi.jpg)