Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Looking with the Mouse

Discussion in 'Scripting' started by XZ001, Oct 23, 2011.

  1. XZ001

    XZ001

    Joined:
    May 21, 2011
    Posts:
    166
    I want to make a script that will rotate the character when I move the mouse side to side and make the first person camera look up and down when I move the mouse up and down. I tried rotating it with the script:

    Code (csharp):
    1. transform.Rotate(0, Input.GetAxis("Mouse X") * rotatespeed, 0);
    But nothing happened. No error messages either, it played fine but the character didnt move. How can I make him look around?
     
  2. All_American

    All_American

    Joined:
    Oct 14, 2011
    Posts:
    1,528
    Her this is a nice clean way to do it-

    Its C#-

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. /// MouseLook rotates the transform based on the mouse delta.
    5. /// Minimum and Maximum values can be used to constrain the possible rotation
    6.  
    7. /// To make an FPS style character:
    8. /// - Create a capsule.
    9. /// - Add the MouseLook script to the capsule.
    10. ///   -> Set the mouse look to use LookX. (You want to only turn character but not tilt it)
    11. /// - Add FPSInputController script to the capsule
    12. ///   -> A CharacterMotor and a CharacterController component will be automatically added.
    13.  
    14. /// - Create a camera. Make the camera a child of the capsule. Reset it's transform.
    15. /// - Add a MouseLook script to the camera.
    16. ///   -> Set the mouse look to use LookY. (You want the camera to tilt up and down like a head. The character already turns.)
    17. [AddComponentMenu("Camera-Control/Mouse Look")]
    18. public class MouseLook : MonoBehaviour {
    19.  
    20.     public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
    21.     public RotationAxes axes = RotationAxes.MouseXAndY;
    22.     public float sensitivityX = 15F;
    23.     public float sensitivityY = 15F;
    24.  
    25.     public float minimumX = -360F;
    26.     public float maximumX = 360F;
    27.  
    28.     public float minimumY = -60F;
    29.     public float maximumY = 60F;
    30.  
    31.     float rotationY = 0F;
    32.    
    33.  
    34.          
    35.    
    36.  
    37.     void Update ()
    38.     {
    39.        
    40.    
    41.  
    42.         if (axes == RotationAxes.MouseXAndY)
    43.         {
    44.             float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
    45.            
    46.             rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
    47.             rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
    48.            
    49.             transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
    50.         }
    51.         else if (axes == RotationAxes.MouseX)
    52.         {
    53.             transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
    54.         }
    55.         else
    56.         {
    57.             rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
    58.             rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
    59.            
    60.             transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0);
    61.         }
    62.     }
    63.    
    64.     void Start ()
    65.     {
    66.     //if(!networkView.isMine)
    67.         //enabled = false;
    68.    
    69.         // Make the rigid body not change rotation
    70.         //if (rigidbody)
    71.             //rigidbody.freezeRotation = true;
    72.     }
    73. }
     
    Chrism24747, soundmartell and kahdeg like this.
  3. JaamiParvez

    JaamiParvez

    Joined:
    Feb 19, 2014
    Posts:
    1
    How to Set the mouse look to use LookY ??
     
  4. MysteriX

    MysteriX

    Joined:
    Apr 8, 2014
    Posts:
    54
    Select the MouseLook-Script in the inspector. There you have several properties. There you can set it.
     
  5. soundmartell

    soundmartell

    Joined:
    Oct 31, 2013
    Posts:
    16
    Thank you. It worked for me.
     
  6. username123445

    username123445

    Joined:
    Dec 1, 2018
    Posts:
    6
  7. Chrism24747

    Chrism24747

    Joined:
    Feb 25, 2020
    Posts:
    1
    thank you