Home Unity

Adjusting Player Orienation to the level

SuperSparkplug
polycounter lvl 9
Offline / Send Message
SuperSparkplug polycounter lvl 9
Hi,

So in my game I have a player going through doors. Each time a door is open, it spawns a new room on the other side and the camera adjusts itself to the new room. The problem is that these doors/rooms go in different angles and directions and the player orientation doesn't update. For example, going into a door on the left causes problems because now what is usually forward is now diagonally upwards in relation to the new room. Here's the part of my code that controls character movement. How do I make it adjust every time a new door is opened.
[LIST=1]
[*]void OnAnimatorMove()
[*]    {
[*]       if(anim)
[*]       {
[*]         Vector3 newPosition = transform.position;
[*]         newPosition.z += anim.GetFloat("speed")* meshMoveSpeed * Time.deltaTime;
[*]         newPosition.x += anim.GetFloat("direction") * meshMoveSpeed * Time.deltaTime;
[*]         if (Mathf.Abs(Input.GetAxis("Horizontal")) < 0.0001 && Mathf.Abs(Input.GetAxis("Vertical")) < 0.0001) {
[*]          transform.forward = lastdirection;
[*]         }
[*]         else {
[*]          transform.forward = Vector3.Normalize(new Vector3(Input.GetAxis("Horizontal"), 0.0f, Input.GetAxis("Vertical"))); // Normalizes the rotation of Z local while moving
[*]          lastdirection = Vector3.Normalize(new Vector3(Input.GetAxis("Horizontal"), 0.0f, Input.GetAxis("Vertical")));
[*]         }
[*]         transform.position = newPosition;
[*]       }
[*]    }
[/LIST]

Replies

  • MikeSeguin
    Options
    Offline / Send Message
    I'm assuming you have your player inside of an empty parent game object, right?

    When you enter each new room, have a trigger volume adjust the rotation of the base game object, thus affecting the child's (in this case, your character's) movement directions as it is parented to it.

    So, like this:

    Main empty parent prefab:
    This is what's re-oriented when you enter a new room. has the actual character prefab parented to it.

    Character/Child Prefab:
    This is the game object that your movement controls interface with. As it is parented to the main game object, any translations/rotations to the parent object adjust what is 'north' for player movement.
Sign In or Register to comment.