0
\$\begingroup\$

I need to calculate the dot product between my character movement direction and the input being pressed by the player. When the character movement was being controlled only by code, I was using something like this:

movementInputVector = new Vector3(inputDirection.y, 0, inputDirection.x) * Time.fixedDeltaTime;
rigidbody.MovePosition(transform.position + movementInputVector);

With this movement code, it was possible to calculate the dot product using the character's tansform.forward and the movement input vector.

It happens that I discovered that my movement needs to use root motion and after this change, I can't find a way to generate a consistent dot product anymore. Already did some attempts, but none of them worked.

Firstly, I tried to use the transform.forward, as it did worked before:

MovementDirectionDot = Vector3.Dot(transform.forward, movementInputVector);

But the result was 0, no matter the direction I pressed as input.

Then I tried to use the root bone of the character as reference:

MovementDirectionDot = Vector3.Dot(rootBoneTransform.forward, movementVector);

This attempt generated inconsistent values from ~-0,07 to ~9.

Taking a look at the transform.forward values, I've noticed it doesn't change as the character moves, remaining as a vector3(0,0,1) the whole time. Also, I tried to normalize all vectors involved and the same results happened.

So I have no clue what to do now, I don't know what vector should be used in this method in order to get back the correct dot product ranging from -1 to 1. Does anyone have an insight on this?

\$\endgroup\$
2
  • \$\begingroup\$ What values do you get from rootBoneTransform.forward, rootBoneTransform.parent.forward, and rigidbody.rotation * Vector3.forward? \$\endgroup\$
    – DMGregory
    Aug 21 '20 at 21:21
  • \$\begingroup\$ Thanks for the answer, here's an image showing what you asked - imgur.com/a/fuNSps4 - I actually changed my root bone, I noticed I was using the wrong one (the hip bone) instead of the actual root of the character. Using the correct root I've got better results with the forward and backward inputs (0,99 and -0,99 respectively) but if I click to any sideways input, the dot gets with strange value again. Apparently I'm close to a solution. \$\endgroup\$ Aug 21 '20 at 22:39
0
\$\begingroup\$

So after revise everything again and again I stopped to take a closer look on two vectors I was using to generate the dot. It happens that the "movementVector" is a vector2 and the "transford.forward" is a vector3, as the movement in a 3D ground happens in the X and Z axis, the results never would be right. So my solution was to create a vector3 using the movementVector as base, inverting the value on X axis(for some reason the original value is opposed of the transform.forward one), so I solved the problem using this 2 lines:

var convertedMovementVector = new Vector3(movementVector.y, 0, -movementVector.x);
    MovementDirectionDot = Vector3.Dot(convertedMovementVector, transform.forward);

The results are consistent now, so I believe this is a good solution for my case, even looking like an improvisation.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.