- Home /
How can we get the camera to find the mouse position and then point the camera at the poistion
using UnityEngine; using System.Collections;
public class MouseFollower : MonoBehaviour {
public float renderDepth = 10.0f;
void Start () {
Screen.showCursor = false;
if(rigidbody) {
rigidbody.freezeRotation = true;
}
}
void Update () {
var mousePos = Input.mousePosition;
var wantedPos = Camera.current.ScreenToWorldPoint(Vector3(mousePos.x, mousePos.y, renderDepth));
transform.position = wantedPos;
}
}
thats the current code we are using but it isnt quite working we keep getting errors and dont know why. Thank you for any and all help :D
by any chance, are you trying to move the camera with your mouse movement? like an FPS would?
This is a great third person cam. http://wiki.unity3d.com/index.php?title=$$anonymous$$ouseOrbitImproved
Answer by robertbu · Aug 18, 2014 at 09:21 PM
You are moving the camera to that position. To just look at that position, change the last line to:
transform.LookAt(wantedPosition);
If you are looking to move the camera (rather than rotate the camera), let me know.
You need a 'new' in front of the Vector3() on line 15. The line should be:
Vector3 wantedPos = Camera.current.ScreenToWorldPoint(new Vector3(mousePos.x, mousePos.y, renderDepth));
Note I've changed 'var' to Vector3, though var will work.
Your answer
Follow this Question
Related Questions
camera movements very slow, why!? :) 2 Answers
How can you make a FPS controller camera "fly"? 1 Answer
Mouse Camera Control 0 Answers
Teleporting a camera to a camera 1 Answer
Camera 2D Movement android unity 1 Answer