Search Unity

Object not rotating towards mouse.

Discussion in 'Scripting' started by Theonesuperx, Aug 13, 2018.

  1. Theonesuperx

    Theonesuperx

    Joined:
    Sep 19, 2013
    Posts:
    24
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class LookAtMouse : MonoBehaviour
    5. {
    6.     public Transform target;
    7.     // float mouse_pos;
    8.     Camera cam;
    9.  
    10.     public float speed = 5f;
    11.  
    12.     private void Start()
    13.     {
    14.         cam = GetComponent<Camera>();
    15.     }
    16.  
    17.     void Update()
    18.     {
    19.         var aimPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    20.          Vector3 direction = transform.position - aimPos;
    21.  
    22.  
    23.  
    24.         // var aimTarget = ray.GetPoint(dist);
    25.  
    26.         //  Vector3 direction = aimTarget - transform.position;
    27.         Quaternion rotation = Quaternion.LookRotation(direction);
    28.         transform.rotation = Quaternion.Lerp(transform.rotation, rotation, speed * Time.deltaTime);
    29.     }
    30. }
    My object, which is the player's gun, won't rotate towards the mouse. This is the code I have, but I am not sure what is wrong. Am I missing something or doing something wrong?
     
  2. Scabbage

    Scabbage

    Joined:
    Dec 11, 2014
    Posts:
    268
    If you already have a reference to your camera then you should be using that, not Camera.main.

    Secondly, the z component of Input.mousePosition is 0. The z component of the vector passed into ScreenToWorldPoint is how far out (along the cameras local z axis) the world point is from the camera. If that's 0, your ray will have length 0. Set it to some nonzero value like this:
    Code (CSharp):
    1. Vector3 mousePos = Input.mousePosition;
    2. mousePos.z = cam.nearClipPlane;
    3. Vector3 direction = cam.ScreenToWorldPoint(mousePos) - transform.position;
     
    Theonesuperx likes this.
  3. Theonesuperx

    Theonesuperx

    Joined:
    Sep 19, 2013
    Posts:
    24
    Well, I tried what you suggested, but I am still having problems.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class LookAtMouse : MonoBehaviour
    5. {
    6.     public Transform target;
    7.     // float mouse_pos;
    8.     Camera cam;
    9.  
    10.     public float speed = 5f;
    11.  
    12.     private void Start()
    13.     {
    14.         cam = GetComponent<Camera>();
    15.     }
    16.  
    17.     void Update()
    18.     {
    19.         Vector3 mousePos = Input.mousePosition;
    20.         mousePos.z = cam.nearClipPlane = 5.5f;
    21.         Vector3 direction = cam.ScreenToWorldPoint(mousePos) - transform.position;
    22.  
    23.  
    24.  
    25.         // var aimTarget = ray.GetPoint(dist);
    26.  
    27.         //  Vector3 direction = aimTarget - transform.position;
    28.         Quaternion rotation = Quaternion.LookRotation(direction);
    29.         target.rotation = Quaternion.Lerp(transform.rotation, rotation, speed * Time.deltaTime);
    30.     }
    31. }
    I am now getting this error

    MissingComponentException: There is no 'Camera' attached to the "Gun" game object, but a script is trying to access it.
    You probably need to add a Camera to the game object "Gun". Or your script needs to check if the component is attached before using it.
    LookAtMouse.Update () (at Assets/LookAtMouse.cs:20)


    Any idea? I have already dragged an object into the target, but it does not appear to fix the problem.
     
  4. Scabbage

    Scabbage

    Joined:
    Dec 11, 2014
    Posts:
    268
    Your start method assumes there's a camera attached to whatever object this script is on. If there is no camera then it will return null. If your cam variable is assigned in the inspector then simply remove line 14 of your script.
     
    Theonesuperx likes this.
  5. Theonesuperx

    Theonesuperx

    Joined:
    Sep 19, 2013
    Posts:
    24

    Oh wow! It was something that simple that went over my head. Thank you so much! You saved me.
     
  6. Theonesuperx

    Theonesuperx

    Joined:
    Sep 19, 2013
    Posts:
    24
    I realize the gun is now backwards. How can I fix that?
     
  7. Scabbage

    Scabbage

    Joined:
    Dec 11, 2014
    Posts:
    268
    If it's multiple objects in a heirarchy, simply spin the mesh 180 degrees on its y axis. If it's just a single object then you could just flip the vector in the rotatation calculation:
    Code (csharp):
    1. Quaternion.LookRotation(-direction);
    But it will generally help in the long run to have an objects local z axis correspond to what makes sense for "forward" to mean, y to correspond to "up", and x to "right".
     
    Theonesuperx likes this.
  8. Theonesuperx

    Theonesuperx

    Joined:
    Sep 19, 2013
    Posts:
    24
    Ah, alright. thanks