Face mouse position in the world when attacking

Hi,

For my implementation I want the player to rotate towards the mouse direction and only allow combo to succeed if the following click is within the time and in certain max angle difference.

Circle is the click point and I want to limit the range like this, so that player can slightly adjust the direction
image

I am getting a mouse position by:
Vector3 mousePoint = Camera.main.ScreenToWorldPoint(Mouse.current.position.ReadValue());

I set the direction:

Vector3 direction = mousePoint - stateMachine.transform.position;

Noticed in other rotation we do we set the y to 0:

direction.y = 0f;

And then I try to rotate:

stateMachine.transform.rotation = Quaternion.LookRotation(direction);

What am I doing wrong? The player object is rotated south on each click.

EDIT:

For some reason with just this code no matter where I click I am getting same value like:

(0.00, 16.06, -8.50)

 Vector3 mousePos = Mouse.current.position.ReadValue();
        Vector3 mouseWorldPos = Camera.main.ScreenToWorldPoint(mousePos);
        Debug.Log (mouseWorldPos);

This is because direction is not a true “rotation”. That’s represented by a Quaternion, and we really don’t want to manipulate those directly. The direction is more of a line, like on a graph. by zeroing out the Y component of this, we’re only looking at the changes in the X and Y values of the direction, effectively taking the slope out of the equation. If we don’t, then when we face the character, the character will actually look like he’s tipping over if the target is at a different elevation.

Try this instead:

Vector3 GetMousePositionInWorld()
{
     Ray ray = Camera.Main.ScreenPointToRay(Mouse.current.position.ReadValue());
     if(Physics.Raycast(ray, out Hit hit)
    {
          Debug.Log($"Mouse is at {hit.Point}");
          return hit.Point;
    }
    Debug.Log("No hits with mouse");
    return Vector3.zero;
}

Thank you @Brian_Trotter based on your answer I came to this solution and it seems to be working:

    private void Attack()
    {
        Vector3 mousePosition = GetMousePositionInWorld();
        Vector3 direction =
            (mousePosition - stateMachine.transform.position).normalized;

        // rotate towards direction
        stateMachine.transform.rotation = Quaternion.LookRotation(direction);

        stateMachine
            .Animator
            .CrossFadeInFixedTime(attack.AnimationName,
            attack.TransitionDuration);
    }

    private Vector3 GetMousePositionInWorld()
    {
        int GroundLayer = 1 << 6;
        Ray ray =
            Camera.main.ScreenPointToRay(Mouse.current.position.ReadValue());

        if (Physics.Raycast(ray, out RaycastHit hit, GroundLayer))
        {
            Debug.Log(hit.transform.name);
            Debug.Log($"Mouse is at {hit.point}");
            return hit.point;
        }

        return Vector3.zero;
    }

I have also used this guide: How to convert the mouse position to world space in Unity (2D + 3D) - Game Dev Beginner

Now I just need to set a max radius from the click that will allow for combo.

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms