Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Make player rotate based on the mouse position in New input system/Character Controller

Hey guys, I've been trying to make a top down game with the new input system. Been trying to make the character rotate and look at the mouse's position. The player moves on the x and z axis, with the camera looking down from the y axis. I have basic movement down, but I can't get the character rotation working.

Rotation is handled within this method in my code: where mousePosition can get he mouse position correctly, and then mouseViewportPosition converts it to a vector3 for the player to use.

Still can't figure out how to get the player to look at the mouse position as rotation with the input system, essentially using the mouse to aim direction. 

I'm running ver. 2021.1.15f1 if that helps.

Any help with this would be greatly appreciated! I'm new to unity and I've been stuck on trying to figure out this for weeks.

My current result looks like this:

Full code here if you want to look at it with the method:

With my player Input Actions here:

Here's the full method code that handles rotation and aim with the mouse, this method is called within Update():

void handleRotation()
         // We're getting a Vector2, whereas we will need a Vector3
         // Get a z value based on camera, and include it in a Vector3
         Vector2 mousePosition = playerInput.CharacterControls.MousePosition.ReadValue<Vector2>();
         var mousePositionZ = _camera.farClipPlane * .5f;
         Vector3 mouseViewportPosition = _camera.ViewportToWorldPoint(new Vector3(mousePosition.x, mousePosition.y, _camera.transform.position.y));
         Debug.Log("MousePos: " + mouseViewportPosition);
         Vector3 positionToLookAt;
         positionToLookAt.x = mouseViewportPosition.x;
         positionToLookAt.y = 0.0f;
         //positionToLookAt.z = currentMovement.z;
         positionToLookAt.z = mouseViewportPosition.z;
         Quaternion currentRotation = transform.rotation;
         Quaternion targetRotation = Quaternion.LookRotation(positionToLookAt - transform.position);
         transform.rotation = Quaternion.Slerp(currentRotation, targetRotation, rotationFactorPerFrame * Time.deltaTime);

Sign In or Register to comment.