Make character look at mouse with New input system/Character Controller

Hello,
I’m fairly new to unity so sorry for any inconvenience to anyone but I’ve been stuck on this for almost a week.
I’m on Unity 2021.1.15f1 if that helps.

Been following this guide

for Character Controller. This got me getting basic movement working and at time 21:20 he creates a way to rotate the player.

and this video for the player facing the mouse position. At the 7:15 mark she explains how to get the mousePosition in which I stored it in positionToLookAt
As well as this video youtu.be/H2RLv0aWUB0?t=1153

I want my character’s rotation to be done with the mouse and have a field of view like here:

Been trying to to get it so that the player is facing the direction of the mouse position, but haven’t been getting the result. With here showing https://imgur.com/gallery/mPPWogi the video results and my PlayerInputs.

Been asking around on other forums and asking around, but had no luck and I’m stuck

Here’s 2 snippets on my how I’m trying to do my version of handleRotation()
Sorry with my messing commenting of code. Been trying for ages and pulling my hair out for this.

I’ll also upload the file itself that was based on the first video.
If anyone wants the project itself, I can upload it to a google drive.
https://drive.google.com/drive/folders/1ihXTOcWbui_i0nNmkqR1SVGssvCIdcHV?usp=sharing

   void handleRotation()
    {
        Vector3 positionToLookAt;

        // get mouse position
        Vector2 mousePosition = playerInput.CharacterControls.MousePosition.ReadValue<Vector2>();
        mousePosition = Camera.main.ScreenToWorldPoint(mousePosition);

        // insert mouse position to looking position
        positionToLookAt.x = currentMovement.x;
        //positionToLookAt.x = mousePosition.x;
        positionToLookAt.y = 0.0f;
        positionToLookAt.z = currentMovement.z;
        //positionToLookAt.z = mousePosition.y;

        Quaternion currentRotation = transform.rotation;

        if (isMovementPressed)
        {
            Quaternion targetRotation = Quaternion.LookRotation(positionToLookAt);
            transform.rotation = Quaternion.Slerp(currentRotation, targetRotation, rotationFactorPerFrame * Time.deltaTime);
        }

    }

And the second one:

 void handleRotation2()
    {
        //Vector3 positionToLookAt;

        Vector2 mouseScreenPosition = playerInput.CharacterControls.MousePosition.ReadValue<Vector2>();
        Vector3 mouseWorldPosition = cam.ScreenToWorldPoint(mouseScreenPosition);
        Vector3 targetDirection = mouseWorldPosition - transform.position;

        Vector3 positionOnViewport = cam.WorldToViewportPoint(transform.position);

        float angle = AngleBetweenTwoPoints(positionOnViewport, targetDirection);

        transform.rotation = Quaternion.Euler(new Vector3(0f, -angle, 0f));

        //float angle = Mathf.Atan2(targetDirection.y, targetDirection.x) * Mathf.Rad2Deg;
        //transform.rotation = Quaternion.Euler(new Vector3(0f, 0f, angle));

        //Debug.Log("MousePos: " + mousePosition.x + " , " + mousePosition.y);

        //positionToLookAt.x = currentMovement.x;
        //positionToLookAt.x = mousePosition.x;
        //positionToLookAt.y = 0.0f;
        //positionToLookAt.z = currentMovement.z;
        //positionToLookAt.z = mousePosition.y;

        //Quaternion currentRotation = transform.rotation;


        //Quaternion targetRotation = Quaternion.LookRotation(targetDirection);
        //transform.rotation = Quaternion.Slerp(currentRotation, targetRotation, rotationFactorPerFrame * Time.deltaTime);


    }

If anyone would help out, that would be great. And again sorry if I’m new to this, and if this seems messy.

7363463–897140–AnimationAndMovementController.cs (6.95 KB)

2 Likes

Just found this via google, not sure why no one helped out, you documented your issue very well.
Unfortunately i have no answer on this as i am just switching from the old, to the new Input System.
Probably you have solved it by now anyways. Cheers.

Did you find the solution? I have the same issue