Search Unity

Question Rotate Camera on Teleport

Discussion in 'Scripting' started by H_Doyle, Feb 19, 2021.

  1. H_Doyle

    H_Doyle

    Joined:
    Jan 26, 2021
    Posts:
    1
    Hi,

    I am developing a game where the player is able to teleport around the scene using buttons on the screen. The teleport works fine in terms of position, but I need the player to have a particular orientation at each teleport point. I can't get the rotation element of this to work. The players rotation remains the same as before teleporting.

    The player's position is controlled using WASD and the player's rotation using the mouse.

    The code for the teleport buttons is below.

    Any help with this issue would be amazing.

    Thank you.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class TeleportButtons : MonoBehaviour
    6. {
    7.     public GameObject Player;
    8.  
    9.     public GameObject TeleportTarget0;
    10.     public GameObject TeleportTarget1;
    11.     public GameObject TeleportTarget2;
    12.     public GameObject TeleportTarget3;
    13.     public GameObject TeleportTarget4;
    14.     public GameObject TeleportTarget5;
    15.     public GameObject TeleportTarget6;
    16.     public GameObject TeleportTarget7;
    17.  
    18.     public void ResetLocation ()
    19.     {
    20.         Player.transform.localPosition = TeleportTarget0.transform.localPosition;
    21.         Player.transform.localEulerAngles = TeleportTarget0.transform.localEulerAngles;
    22.     }
    23.  
    24.     public void TeleportToLocation1()
    25.     {
    26.         Player.transform.localPosition = TeleportTarget1.transform.localPosition;
    27.         Player.transform.localEulerAngles = TeleportTarget1.transform.localEulerAngles;
    28.     }
    29.  
    30.     public void TeleportToLocation2()
    31.     {
    32.         Player.transform.localPosition = TeleportTarget2.transform.localPosition;
    33.         Player.transform.localEulerAngles = TeleportTarget2.transform.localEulerAngles;
    34.     }
    35.  
    36.     public void TeleportToLocation3()
    37.     {
    38.         Player.transform.localPosition = TeleportTarget3.transform.localPosition;
    39.         Player.transform.localEulerAngles = TeleportTarget3.transform.localEulerAngles;
    40.     }
    41.  
    42.     public void TeleportToLocation4()
    43.     {
    44.         Player.transform.localPosition = TeleportTarget4.transform.localPosition;
    45.         Player.transform.localEulerAngles = TeleportTarget4.transform.localEulerAngles;
    46.     }
    47.  
    48.     public void TeleportToLocation5()
    49.     {
    50.         Player.transform.localPosition = TeleportTarget5.transform.localPosition;
    51.         Player.transform.localEulerAngles = TeleportTarget5.transform.localEulerAngles;
    52.     }
    53.  
    54.     public void TeleportToLocation6()
    55.     {
    56.         Player.transform.localPosition = TeleportTarget6.transform.localPosition;
    57.         Player.transform.localEulerAngles = TeleportTarget6.transform.localEulerAngles;
    58.     }
    59.  
    60.     public void TeleportToLocation7()
    61.     {
    62.         Player.transform.localPosition = TeleportTarget7.transform.localPosition;
    63.         Player.transform.localEulerAngles = TeleportTarget7.transform.localEulerAngles;
    64.     }
    65. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Gotta find out what's going wrong. If your player controller has internal rotation state, or has a character controller or rigidbody, those constructs might drive the rotation back, effectively ignoring the rotation you set on teleport.

    Solution: make a method in the player controller to accept a new rotation and use those methods instead of setting the transform directly. Then inside the player controller, write code to drive the rotation properly.

    Side benefit: it's trivial to make a little test scene that drives the player rotation so you can test it.