Search Unity

  1. Get all the Unite Berlin 2018 news on the blog.
    Dismiss Notice
  2. Unity 2018.2 has arrived! Read about it here.
    Dismiss Notice
  3. We're looking for your feedback on the platforms you use and how you use them. Let us know!
    Dismiss Notice
  4. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  6. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  7. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  8. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Object not rotating towards mouse.

Discussion in 'Scripting' started by Theonesuperx, Aug 13, 2018 at 12:50 AM.

  1. Theonesuperx

    Theonesuperx

    Joined:
    Sep 19, 2013
    Posts:
    20
    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.         transform.rotation = Quaternion.Lerp(transform.rotation, rotation, speed * Time.deltaTime);
    28.     }
    29. }
    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:
    266
    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. mousePos.z = cam.nearClipPlane;
    2. Vector3 direction = cam.ScreenToWorldPoint(mousePos) - transform.position;
     
    Theonesuperx likes this.
  3. Theonesuperx

    Theonesuperx

    Joined:
    Sep 19, 2013
    Posts:
    20
    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.     }
    29. }
    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:
    266
    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:
    20

    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:
    20
    I realize the gun is now backwards. How can I fix that?
     
  7. Scabbage

    Scabbage

    Joined:
    Dec 11, 2014
    Posts:
    266
    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):
    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:
    20
    Ah, alright. thanks