how to make camera move with the mouse cursors
Hello everyone, i searched this but couldn't find the answers i want. The camera doesn't moves with the mouse cursor, i need to make it look where the mouse cursor is, i could find this which is closest to my question :
http://answers.unity3d.com/questions/10006/mouse-camera-camera-mouse.html
but it's about camera position, when i do that to my camera it goes too far somewhere in the scene, i just want camera to look where the cursor is.
Please help,:).
GameObject mycam = GameObject.Find("$$anonymous$$ain Camera");
Vector3 mou;
float senc=1f;
//put this in update function
mou=new Vector3(Input.GetAxis ("right x"),Input.GetAxis ("right y"),0);
mou=mou*senc*Time.deltaTime;
mycam.transform.position+=mou;
Answer by fffMalzbier · Jul 27, 2011 at 12:02 PM
Whats about a simple script on the camera? Just something like:
Camera mycam = GetComponent<Camera>();
transform.LookAt(mycam.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, mycam.nearClipPlane)), Vector3.up);
(hope it work cant proof it on this mac , its CSharp by the way)
is there a way to decrease the sensitivity a bit. I can't really control it.
@Oshoura To control the sensitivity you can use the following code
Camera mycam = GetComponent<Camera>();
float sensitivity = 0.05f;
Vector3 vp = mycam.ScreenToViewportPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, mycam.nearClipPlane));
vp.x -= 0.5f;
vp.y -= 0.5f;
vp.x *= sensitivity;
vp.y *= sensitivity;
vp.x += 0.5f;
vp.y += 0.5f;
Vector3 sp = mycam.ViewportToScreenPoint(vp);
Vector3 v = mycam.ScreenToWorldPoint(sp);
transform.LookAt(v, Vector3.up);
I am new to Unity, so there is maybe a better way of doing it.
Is there some way to stop the camera movement after it has reached some set threshold in all directions. Like if I want my camera not to look straight down or in the back?
Answer by Nexnar · Jul 26, 2018 at 02:53 AM
@rifintidhamar I agree, just as rifintidhamar first implied(and to my elaboration) there are multifaceted approaches and the current method presented can be scrubbed a bit, I suggest a GetAxis Cross Scew
while its clearly not perfect, i found it much more use able than what we currently have for something like a mouse, tell me what you think.
float mouseX = (Input.mousePosition.x / Screen.width ) - 0.5f;
float mouseY = (Input.mousePosition.y / Screen.height) - 0.5f;
transform.localRotation = Quaternion.Euler (new Vector4 (-1f * (mouseY * 180f), mouseX * 360f, transform.localRotation.z));
Answer by JaackFord · Jan 22, 2018 at 11:51 PM
@joeranbenker where is that code supposed to go? update or start?,@joeranbenker Where is that sensitivity code supposed to go? which method? Cause right now that changes nothing for me.
Update ofcourse. if you put it in start, it will only work the first frame of the game.
Answer by rifintidhamar · Jul 25, 2018 at 08:42 PM
this method is really only useful for a joystick, and even then, using the get axis method is probably a better idea.
Your answer
Follow this Question
Related Questions
Make the camera rotate around the player and face the player. 1 Answer
Please help me with my mouse pointer 0 Answers
CATCH MOUSE CLICK EVENT ON OBJECTS 0 Answers
Rotate towards mouse pointer 1 Answer