using UnityEngine;
using System.Collections;
public class LookAtMouse : MonoBehaviour
{
public Transform target;
// float mouse_pos;
Camera cam;
public float speed = 5f;
private void Start()
{
cam = GetComponent<Camera>();
}
void Update()
{
var aimPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector3 direction = transform.position - aimPos;
// var aimTarget = ray.GetPoint(dist);
// Vector3 direction = aimTarget - transform.position;
Quaternion rotation = Quaternion.LookRotation(direction);
transform.rotation = Quaternion.Lerp(transform.rotation, rotation, speed * Time.deltaTime);
}
}
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?
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:
Vector3 mousePos = Input.mousePosition;
mousePos.z = cam.nearClipPlane;
Vector3 direction = cam.ScreenToWorldPoint(mousePos) - transform.position;
1 Like
Well, I tried what you suggested, but I am still having problems.
using UnityEngine;
using System.Collections;
public class LookAtMouse : MonoBehaviour
{
public Transform target;
// float mouse_pos;
Camera cam;
public float speed = 5f;
private void Start()
{
cam = GetComponent<Camera>();
}
void Update()
{
Vector3 mousePos = Input.mousePosition;
mousePos.z = cam.nearClipPlane = 5.5f;
Vector3 direction = cam.ScreenToWorldPoint(mousePos) - transform.position;
// var aimTarget = ray.GetPoint(dist);
// Vector3 direction = aimTarget - transform.position;
Quaternion rotation = Quaternion.LookRotation(direction);
target.rotation = Quaternion.Lerp(transform.rotation, rotation, speed * Time.deltaTime);
}
}
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.
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.
1 Like
Oh wow! It was something that simple that went over my head. Thank you so much! You saved me.
I realize the gun is now backwards. How can I fix that?
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:
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â.
1 Like