- Home /
Question about angles.
Hello,
I'm currently working on the player's movements ( first project ) and I'm facing some problems with Quaternions, so there is my problem :
Black capsule : the player. Red circle : player's feet.
The player can dash towards mouse position, when the player enters in a wall collider I want his feet to be against the wall. ( so the player transform needs to be rotated on the z axis )
So I tried to compute the Angle between the feet and collision point ... but I always got an unexpected angle.
There is my code :
private void BotSideOnMapEntity (Collision collision)
{
Vector3 collisionPoint = collision.contacts[0].point;
float angleBetween = Vector3.Angle(jumpStartMarker.transform.position, collisionPoint);
transform.rotation *= Quaternion.Euler(0, 0, angleBetween);
}
Thanks for your help !
Answer by TheKnightsofUnity · Nov 23, 2018 at 11:42 AM
Hello there, @CCnockaert!
For this kind of rotation calculation, we would suggest to determine the closest point from the center of the player to the collider using Collider.ClosestPoint method, then calculate the angle between that point and Vector2.up using Vector2.SignedAngle.
private void BotSideOnMapEntity(Collision collision)
{
Vector3 closestPoint = collision.collider.ClosestPoint(transform.position);
float angle = Vector2.SignedAngle(Vector2.up, transform.position - closestPoint);
transform.rotation = Quaternion.Euler(0, 0, angle);
}
This should let the player rotate, so they would have their feet against the obstacle. This should also work for different kinds of colliders, not only the BoxCollider.
All the best,
The Kinghts of Unity