-1
\$\begingroup\$

I have third person view on my character already, but I can't figure out how to make my camera rotate my player using Unity.

\$\endgroup\$
1
  • \$\begingroup\$ How have you set up your scene & scripts so far? And in what way do you want the mouse to rotate the character? (Eg. are you mapping left-right mouse motion to yaw, or is this a point & click style interaction, or something else?) \$\endgroup\$
    – DMGregory
    Aug 20, 2018 at 21:44

1 Answer 1

0
\$\begingroup\$

I assume that you know about Layer Masks so I'm going to jump directly to the piece of code and explanation:

enter image description here

So I created a parent project which holds the script called "PlayerController" and the capsule model.Then we have two children: Cube is the red one that I have added just to see the change of the direction of the Player. Player plane is actually responsible to check the raycast that we will cast to see the difference between the mouse position and the player position, let's see how :

private RaycastHit _hit;

private void Update()
{
    var mousePosition = Input.mousePosition;
    var cameraRay = Camera.main.ScreenPointToRay(mousePosition);
    var layerMask = LayerMask.GetMask("Ground");

    if (Physics.Raycast(cameraRay, out _hit, 100, layerMask))
    {
        var forward = _hit.point - this.transform.position;
        var rotation = Quaternion.LookRotation(forward);

        this.transform.rotation = new Quaternion(0, rotation.y, 0, rotation.w).normalized;
    }
}

enter image description here

Don't forget to add a layer called "Ground" and change the layer of the PlayerPlane object to that new Layer you just created.

I hope this help you.

Happy Coding!

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .