Make my Gunpoint aim/rotate on the z axis towards my mouse cursor

Hello community


I’m making a 2.5D sidescrolling game (2d gameplay so only they cannot move on the Z position), and trying to implement that the character can throw an axe in an arc. The arc part is done by adding gravity to the projectile and a certain speed when the projectiles are instantiated.


However, I’m stuck on aiming with the mouse.

I’ve tried various codes such as the two below:

public float speed = 5f;

private void Update()
{
    Vector2 direction = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
    float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
    Quaternion rotation = Quaternion.AngleAxis(angle, Vector3.forward)
}

and

    void Update()
    {
        Vector3 direction = Input.mousePosition - transform.position;
        Quaternion rotation = Quaternion.LookRotation(direction);
        transform.rotation = rotation;
                    
    }

The closest to remotely make it rotate was just to write the following code:

        Vector3 direction = Input.mousePosition - transform.position;

But doing this doesn’t take in consideration of the gamescreen, and it seems it only makes it rotate on the rotation.x axis, and what I desire is it to rotate on the rotation.z axis.

However, this apparently makes my projectile not go towards the direction its supposed to.

In case my projectile-instantiation codes are relevant:

 void Update()
    {

        if (timeBtwAttack <= 0)
        {

            player = GameObject.Find("Player").GetComponent<Player>();

        if (Input.GetKeyDown(KeyCode.Mouse1))

        {
            if(player.facingRight == true)
            { 
            GameObject fireballInstance;
            fireballInstance = Instantiate(throwingAxe, spawnPoint.position, spawnPoint.rotation) as GameObject;
            fireballInstance.GetComponent<Rigidbody2D>().AddForce(spawnPoint.right * speed);
            }
            else
            {
                GameObject fireballInstance;
                fireballInstance = Instantiate(throwingAxe, spawnPoint.position, Quaternion.Euler(new Vector3(0, 0, 0))) as GameObject;
                fireballInstance.GetComponent<Rigidbody2D>().AddForce(-spawnPoint.right * speed);
            }
                timeBtwAttack = startTimeBtwAttack;

        }

        }
        else
        {
            timeBtwAttack -= Time.deltaTime;
        }

    }

Hope somebody can guide me in the right way.

Ok, so the vector3 you feed in to the ScreenToWorldPoint() method needs to have a z value that represents the distance from the camera to your player. With this in mind you can add to your code and end up with something like this

void Update()
    {
        //The distance from your player to the camera
        float camToPlayerDist = Vector3.Distance(transform.position, Camera.main.transform.position);

        //This is the world position of your mouse
        Vector2 mouseWorldPosition = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, camToPlayerDist));

        //The direction your mouse is pointing in with relation to your player
        Vector2 direction = mouseWorldPosition - (Vector2)transform.position;

        //the angle of your direction
        float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;

        //Setting the rotation to the transform.
        transform.rotation = Quaternion.Euler(0, 0, angle);
    }

The transform you add this code to will be doing the rotating so make sure you set the rotation of the “weapon” you want to rotate. In my example I rotate whatever transform this script is attached to. Hope this helps