- Home /
mouse look script
how can i get my camera to follow mouse? can someone please tell me the sript
HI It is cool to see this but I have a problem with Oculus H$$anonymous$$D IF anyone can help it would be appreciated I want to move as per H$$anonymous$$D look in scene and i think $$anonymous$$ouse look script would be helpful but i don't recognize how to do it
looking for kind help Thank you
Answer by IJM · Oct 11, 2010 at 01:28 AM
This is MouseLook.cs:
using UnityEngine; using System.Collections;
/// MouseLook rotates the transform based on the mouse delta. /// Minimum and Maximum values can be used to constrain the possible rotation
/// To make an FPS style character: /// - Create a capsule. /// - Add a rigid body to the capsule /// - Add the MouseLook script to the capsule. /// -> Set the mouse look to use LookX. (You want to only turn character but not tilt it) /// - Add FPSWalker script to the capsule
/// - Create a camera. Make the camera a child of the capsule. Reset it's transform. /// - Add a MouseLook script to the camera. /// -> Set the mouse look to use LookY. (You want the camera to tilt up and down like a head. The character already turns.) [AddComponentMenu("Camera-Control/Mouse Look")] public class MouseLook : MonoBehaviour {
public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
public RotationAxes axes = RotationAxes.MouseXAndY;
public float sensitivityX = 15F;
public float sensitivityY = 15F;
public float minimumX = -360F;
public float maximumX = 360F;
public float minimumY = -60F;
public float maximumY = 60F;
float rotationX = 0F;
float rotationY = 0F;
Quaternion originalRotation;
void Update ()
{
if (axes == RotationAxes.MouseXAndY)
{
// Read the mouse input axis
rotationX += Input.GetAxis("Mouse X") * sensitivityX;
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationX = ClampAngle (rotationX, minimumX, maximumX);
rotationY = ClampAngle (rotationY, minimumY, maximumY);
Quaternion xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
Quaternion yQuaternion = Quaternion.AngleAxis (rotationY, -Vector3.right);
transform.localRotation = originalRotation * xQuaternion * yQuaternion;
}
else if (axes == RotationAxes.MouseX)
{
rotationX += Input.GetAxis("Mouse X") * sensitivityX;
rotationX = ClampAngle (rotationX, minimumX, maximumX);
Quaternion xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
transform.localRotation = originalRotation * xQuaternion;
}
else
{
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = ClampAngle (rotationY, minimumY, maximumY);
Quaternion yQuaternion = Quaternion.AngleAxis (-rotationY, Vector3.right);
transform.localRotation = originalRotation * yQuaternion;
}
}
void Start ()
{
// Make the rigid body not change rotation
if (rigidbody)
rigidbody.freezeRotation = true;
originalRotation = transform.localRotation;
}
public static float ClampAngle (float angle, float min, float max)
{
if (angle < -360F)
angle += 360F;
if (angle > 360F)
angle -= 360F;
return Mathf.Clamp (angle, min, max);
}
}
hello, i know this is an old post and do not really know if I 'can' still comment it, but I would need some litle help on it...
it works perfectly fine, but I attached it directly to the player and not the camera as I am making a 3rd person an not a FPS.
I would like to be able to move on Y axis only the camera and not the player though. How could I change the script to do this?
Thank you!
@grkl30 Let's say your TPS was an FPS. Create an Empty GameObject that represents the camera in the FPS. Then, place the TPS camera behind the Empty GameObject as a Child. Now, attach the $$anonymous$$ouseLook script to the Empty GameObject and it should work somewhat well.
The GameObject would presumably be at the player model's head.
If you're having trouble understanding what I'm describing to you, Comment back and I'll provide a more elaborate explanation.
thanks for your feedback phxvyper, I managed to solve it since though bests
Answer by AndyP-123 · Jan 31, 2016 at 02:33 PM
I found the script in the accepted answer was giving me some weird rotations so I wrote a pretty minimal script that works for my use case quite well (sitting at a chair and looking around, but you should be able to extend it easily for walking etc.)
using System;
using UnityEngine;
public class MouseLook : MonoBehaviour
{
public float mouseSensitivity = 100.0f;
public float clampAngle = 80.0f;
private float rotY = 0.0f; // rotation around the up/y axis
private float rotX = 0.0f; // rotation around the right/x axis
void Start ()
{
Vector3 rot = transform.localRotation.eulerAngles;
rotY = rot.y;
rotX = rot.x;
}
void Update ()
{
float mouseX = Input.GetAxis("Mouse X");
float mouseY = -Input.GetAxis("Mouse Y");
rotY += mouseX * mouseSensitivity * Time.deltaTime;
rotX += mouseY * mouseSensitivity * Time.deltaTime;
rotX = Mathf.Clamp(rotX, -clampAngle, clampAngle);
Quaternion localRotation = Quaternion.Euler(rotX, rotY, 0.0f);
transform.rotation = localRotation;
}
}
I kinda wish Unity had some more minimal components than those in the standard assets. It's easier to understand and learn from simple code, and then you can extend it yourself, and it's REALLY nice not to have a bunch of dependencies to pull in :D
Yeah thanks AndyP, this is exactly what I was looking for. I'm struggling to understand the other one, but this one makes much more sense. :D
I'm wondering why setting mouseSensitivity to 1 gives different results from removing mouseSesitivity altogether. So when I set it to 1, the camera rotates fine, but when I remove it, it barely moves. Shouldn't the two scenarios give the same results since mouseSensitivity is being multiplied?
Edit 1: Never $$anonymous$$d that, the value was being overridden in the inspector.
Answer by Akusan · Sep 01, 2014 at 05:51 AM
MouseLook.cs and other character controllers are available from Unity if you select from the menu bar "Assets" -> Import Package -> Character Controller
After that, the following will be available from the menu bar:
"Component" -> Camera-Control -> Mouse Look
"Component" -> Character -> FPS Input Controller
Answer by Nuclearblast · Apr 09, 2016 at 01:12 PM
Allright this one here from AndyP is great.
The way I made it work was: 1. creating a new empty C# script in my scripts folder. 2. Renamed the script "MouseLookSimple" (to avoid getting it tangled in with the asset script). 3. Attached it to my custom player-character (note that I am not using the Assets FPS). 4. Remember to rename the class in the scrip "MouseLookSimple" in line 4.
Note that the main camera is a child of my Player character.
Answer by asdfghjkla4fnhrnhjtfnfk · Mar 13, 2017 at 06:55 AM
I'm using AndyP's script in a third person shooter and when I use it to rotate the character, it doesn't move in the direction that it is facing, even though I am using transform.localPosition to move it. It could have something to do with the script for the player but I'm only a beginner and wouldn't really know what's going wrong anyway.`
rb = GetComponent<Rigidbody>();
%|1229200423_2|%
// Update is called once per frame
%|1240188770_4|% %|1793753209_5|% if (Input.GetKey("w")) { %|-22954854_8|% %|-1985746889_9|% transform.Rotate(Vector3.forward 0); } if (Input.GetKey("s")) { ; %|1700589554_15|% %|1190135946_16|% } if (Input.GetKey("a")) { ; transform.localPosition += (Vector3.left speed);
}
%|-1911921683_24|% { ; transform.localPosition += (Vector3.right speed); transform.Rotate(Vector3.back 0); %|-185005678_29|% if (Input.GetKey("KeyCode.LeftShift")) { transform.localPosition += (Vector3.forward runspeed); transform.Rotate(Vector3.back 0); }
if (Input.GetKey("KeyCode.Space"))
%|-703131115_36|% %|1705099323_37|% %|1692623434_38|% %|413218732_39|% } }
` The rotation is unnecessary but I might use it in the future so I kept it there.