Search code examples
unity-game-enginerotationunityscript

Change playerCharacter's facing direction in Unity3D


I am trying (for learning purposes) to make a Portal game. I have the basics working, I can place two portals, and walking within the collider of one makes me teleport to the other, however I can't seem to get the facing direction/rotation to work. I want to face outwards from the new portal after the teleportation.

I have tried the following, with no success: var angle = thisPortalCamera.transform.rotation.eulerAngles.y - otherPortalCamera.transform.rotation.eulerAngles.y; playerChar.transform.Rotate(Vector3.up, angle);

My idea here was that only the y-axis rotation really matters, and I think I should rotate the player by the difference in axis between the two portals. This is probably really simple and easy, but I am pretty new to Unity. Any suggestions?


Solution

  • The easiest way would be to set your portal so that its forward is the orientation you want you player to have. Then you just go with:

    player.transform.rotation = portal.transform.rotation;
    player.transform.position = portal.transform.position;
    

    The aim is to have the blue arrow of your portal to point in the right direction.