0
\$\begingroup\$

I have created a Third Person Controller. The camera is behind the player:

enter image description here

I would like to make it so that the player aims at the mouse pointer position.

To do that, I use the following code to rotate the chest towards the position:

        var mousePos = Input.mousePosition;
        mousePos.z = 10; // Make sure to add some "depth" to the screen point 
        var aim = Camera.main.ScreenToWorldPoint(mousePos);
        Chest.LookAt(aim);

The hands do not perfectly straightly point forwards, so I need to add an "offset" to the LookAt.

I tried this approach:

var mousePos = Input.mousePosition;
mousePos.z = 10; // Make sure to add some "depth" to the screen point 
var aim = Camera.main.ScreenToWorldPoint(mousePos);
aim += new Vector3(ChestAimCenterOffsetForLookatBecauseChestIsNotStraightForward, 0, 0);

Chest.LookAt(aim);

Of course I do all rotation patching in LateUpdate(). In Update(), it would simply be erased by the animation itself.

It does work, but when the model is rotated around the Y axis, something goes wrong: The offset is "wrong", it doesn't have the same effect.

This surprises me because all I do is rotate the entire model.

So I think this line is not correct:

aim += new Vector3(ChestAimCenterOffsetForLookatBecauseChestIsNotStraightForward, 0, 0);

Is this not the way to offset the LookAt results?

\$\endgroup\$
10
  • \$\begingroup\$ You should probably be adjusting bones or IK targets, not the mesh objects themselves. However, it's hard to be sure without knowing how the model is rigged. \$\endgroup\$
    – Jim U
    Commented Aug 16, 2020 at 21:28
  • \$\begingroup\$ @JimU I have edited my posting and explained that the rig is perfectly fine and that the twist comes from the aim pose itself. In this aim pose, the chest is just rotated. Maybe I didn't understand you correctly? \$\endgroup\$
    – tmighty
    Commented Aug 16, 2020 at 21:57
  • \$\begingroup\$ Is the statement Chest.LookAt(aim) affecting the model at all? \$\endgroup\$
    – Jim U
    Commented Aug 17, 2020 at 0:50
  • 1
    \$\begingroup\$ Is the model rigged for Inverse Kinematics? \$\endgroup\$
    – Jim U
    Commented Aug 17, 2020 at 0:50
  • \$\begingroup\$ @JimU I wanted to aim using the Chest rotation only, I didn't want to use IK. And yes, Chest.LookAt(aim) does what it should, only it was a bit off. When I read your comment, I took a look at the Chest rotation, and I noticed that it was Y= -15. When I put Y = 0, the aim pos was EXACTELY straight forward. I will add all of this to my posting to make it clear to the reader. Thank you very much so far! \$\endgroup\$
    – tmighty
    Commented Aug 17, 2020 at 21:52

1 Answer 1

2
\$\begingroup\$

If an easy-to-read way to offset the LookAt is required, then the following will work:

Chest.LookAt(aim);
Chest.rotation *= Quaternion.Euler(ChestAimCenterOffsetBecauseHandsDontPointStraightForward.x, ChestAimCenterOffsetBecauseHandsDontPointStraightForward.y, ChestAimCenterOffsetBecauseHandsDontPointStraightForward.z);

According to DMGregory's lesson however this is a more elegant and thus preferred solution with only 1 setter:

Quaternion lookAt = Quaternion.LookRotation(aim - Chest.position);
Quaternion correction = Quaternion.Euler(ChestAimCenterOffset);

Chest.rotation = lookAt * correction;

ps: I'm now using a Vector3 instead because I noticed that the up-down also needs an offset.

\$\endgroup\$
0

You must log in to answer this question.

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