I’m using this code but the rotation is all messed up. How can I get my gun to point wherever my mouse is pointing?
using UnityEngine;
using System.Collections;
public class GunRotate : MonoBehaviour
{
private void Update()
{
Vector3 mousePos = Input.mousePosition;
transform.LookAt(Camera.main.ScreenToWorldPoint(new Vector3(mousePos.x, Screen.height - mousePos.y, 10.0f)));
transform.rotation = new Quaternion(0, 90, transform.rotation.z, transform.rotation.w);
}
}
What I want is the gun’s z axis to be pointing/facing the mouse position so that it’s looking at wherever the mouse position is on the camera. Here’s what I get currently.
I don’t know why you were doing Screen.height - mousePos.y
Also you are already telling it to look at the point, you don’t need to set the rotation again after that.
This may not work 100% because I just typed this into the Quick Reply box. May have to be slightly tweaked to your liking.
I am thinking that its not looking “forward”, so thats why we need to find the forward vector for the transform them increase it (*10) so that it is actually looking at something.