Moodie
Moodie

Reputation: 35

Unity object not rotating to mouse position

So I have script which I got from Blackthornprods ranged combat youtube video since im a beginner. Im trying to get my weapon to rotate around my sphere gameobject but for some reason it doesnt rotate to where the mouse position is, but when I jump it rotates around weirdly not really to the mouse but randomly (im assuming randomly). My game is 3D and his tutorial was for 2d. I would really appreciate any attempt for a solution. Here is my code:

void Update()
{
    Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
    float rotZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
    transform.rotation = Quaternion.Euler(0f, 0f, rotZ + offset);

}

The weapon is still to rotate around the Z axis.

Upvotes: 0

Views: 1099

Answers (1)

Ruzihm
Ruzihm

Reputation: 20249

First, Camera.main calls FindGameObjectsWithTag, which is an expensive operation, so (as the documentation says), you should call it as few times as possible and cache the result:

Camera mainCam;

void Awake()
{
    mainCam = Camera.main;
}

Second, you're using ScreenToWorldPoint incorrectly. If this is really a 3d game as the question describes, you should provide a depth from the camera as the z component of the argument. You can use vector math to do this, and the result is a world position you can tread the cursor as being at.:

Camera mainCam;

void Awake()
{
    mainCam = Camera.main;
}

void Update()
{
    float objectDepthFromCamera = Vector3.Dot(
            transform.position - mainCam.transform.position, 
            mainCam.transform.forward); 

    Vector3 cursorWorldPosition = mainCam.ScreenToWorldPoint(Input.mousePosition 
            + Vector3.forward * objectDepthFromCamera);

Then assuming the "front" of the object points out of the local up direction, you can use the optional second parameter of Quaternion.SetRotation to set the rotation so that is pointing toward the cursor:

Camera mainCam;

void Awake()
{
    mainCam = Camera.main;
}

void Update()
{
    float objectDepthFromCamera = Vector3.Dot(
            transform.position - mainCam.transform.position, 
            mainCam.transform.forward); 

    Vector3 cursorWorldPosition = mainCam.ScreenToWorldPoint(Input.mousePosition 
            + Vector3.forward * objectDepthFromCamera);

    transform.rotation = Quaternion.LookRotation(Vector3.forward,
            cursorWorldPosition - transform.position);
}

If the front of the object points out the local right direction, you can use Vector3.Cross to determine what direction the local up should be pointing:

Camera mainCam;

void Awake()
{
    mainCam = Camera.main;
}

void Update()
{
    float objectDepthFromCamera = Vector3.Dot(
            transform.position - mainCam.transform.position, 
            mainCam.transform.forward); 

    Vector3 cursorWorldPosition = mainCam.ScreenToWorldPoint(Input.mousePosition 
            + Vector3.forward * objectDepthFromCamera);

    Vector3 localUpNeeded = Vector3.Cross(Vector3.forward, 
            cursorWorldPosition - transform.position);

    transform.rotation = Quaternion.LookRotation(Vector3.forward, localUpNeeded);
}

Upvotes: 1

Related Questions