- Home /
Camera follow mouse around the map
Hello there. So i am trying to make the camera follow the mouse. But when i did this:
using UnityEngine;
using System.Collections;
public class CamMouse : MonoBehaviour {
void Update () {
transform.position = Input.mousePosition;
}
}
That worked fine with moving to the sides. But it zooms in and out when moving the mouse up and down.
So then i figured that this will work:
using UnityEngine;
using System.Collections;
public class CamMouse : MonoBehaviour {
void Update () {
transform.position.x = Input.mousePosition.x;
transform.position.z = Input.mousePosition.y;
}
}
But that isnt working either.
So how do i fix this?
Frederik
@nventimiglia what do you mean by posting these pointless comments? Are you contributing anything, or imparting any information that could be useful and informative to the asker and future readers? If you would like to know, why don't you ask a question and post some code that you have written? I am not going to be presumptious as to what the asker wants to do to modify the values from the mouse position, but if you check any of my answers, I shall do my best to answer that. Try to be informative and constructive please.
Im trying to help and you rant off on me ? wtf dude ?
I had my tool tip and software cursor scripts ready to share /if/ they were applicable... Guess not.
maybe the tone of Uuum what about converting the mouse from screen-space first ? was mis-interpreted by me, if so I apologize. Please don't let my error affect the other users from learning and benefiting from knowledge. Your choice.
Answer by termway · Jul 05, 2012 at 05:20 PM
Hi !
You have to create a new vector to assign the new position
public class CamMouse : MonoBehaviour
{
public void Update()
{
transform.position = new Vector3(
Input.mousePosition.x,
Input.mousePosition.y,
0);
}
}
Uuum what about converting the mouse from screen-space first ?
yep =]
Build the new Vector3 with ( mouseX , 0 , mouseY )
But it zooms in and out when moving the mouse up and down. transform.position.z = Input.mousePosition.y;
oh, and to keep the Y-axis the same, use :
new Vector3( Input.mousePosition.x, transform.position.y, Input.mousePosition.y );
Answer by AlucardJay · Jul 05, 2012 at 05:06 PM
create a Vector3 temporary variable first, then use that to assign a value to transform.position.
using UnityEngine;
using System.Collections;
public class CamMouse : MonoBehaviour {
void Update () {
//transform.position.x = Input.mousePosition.x;
//transform.position.z = Input.mousePosition.y;
// error CS1612: Cannot modify a value type return value of `UnityEngine.Transform.position'.
// Consider storing the value in a temporary variable
Vector3 thePos = new Vector3( Input.mousePosition.x, transform.position.y, Input.mousePosition.y ); // temporary variable
transform.position = thePos;
}
}
Uuum what about converting the mouse from screen-space first ?
@nventimiglia Uuum, that wasn't what the person was asking =]