I have a third person character using the character contoller and a box collider. I want to make a setup where if you enter the collider from any angle the player character will face exactly the local Y axis of the collider.
The OnTriggerEnter function itself is working but I can’t figure out the code that would do the rotation alignment.
Just to clarify – the character’s local forward axis (+z) will align to the trigger’s local upwards axis (+y)? So a character walking into an unrotated box will rotate flat onto their back and look straight up?
What I need is for the character (+Z) to align to the (+Z) of the trigger.
The reason for that is because I am making a third person cover system. So when you enter a collider box (with IsTrigger on) behind a wall the character will face the wall directly so that if the move left or right while in cover mode they will move on a line that is exactly parallel to the wall/trigger.
Quaternion.LookRotation has an optional second parameter to specify an upward direction. If you make the character’s Z axis point in the same direction as the trigger’s, you can still control the direction of the character’s Y axis.
As you can see the “cover mode” starts the moment you enter the trigger. In cover mode you can only move left and right.
But if you enter from an angle like the third screenshot you will not be positioned correctly.
So the idea is to add a script to the player that detects if he entered a game object tagged “trigger” and then smoothly rotate itself so that it faces the the direction that the trigger is facing.
So no matter what angle you enter the trigger itself you will be positioned and rotate correctly if you use the cover system.