Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Offsetting transform.position by local rotation

Discussion in 'Scripting' started by kyleyoungblom, Aug 19, 2015.

  1. kyleyoungblom

    kyleyoungblom

    Joined:
    May 1, 2014
    Posts:
    29
    I'm using a script to move my player object between exterior/interior setpieces. I want to move the player from Trigger A to Trigger B, but offset Player position a few units on Trigger B's Z axis so the player doesn't arrive in the trigger. I'm not sure how to transform the local rotation of Trigger B into an offset in global space, though.

    Code (CSharp):
    1.  
    2.     private GameObject Player;
    3.     public GameObject Destination;
    4.  
    5.     // Use this for initialization
    6.     void Start ()
    7.     {
    8.         if (Player == null)
    9.             Player = GameObject.FindWithTag("Player");
    10.     }
    11.    
    12.     void OnTriggerEnter(Collider other)
    13.     {
    14.         Player.transform.position = Destination.transform.position;
    15.     }
    16.  
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    Code (csharp):
    1.  
    2. ... = Destination.transform.position + (Destination.transform.forward * someScalarValue);
    3.  
    (always getting my axes mixed up; replace "forward" with "up" or "right" or negatives of them to get whichever direction you need)
     
    kyleyoungblom likes this.
  3. kyleyoungblom

    kyleyoungblom

    Joined:
    May 1, 2014
    Posts:
    29
    perfect, thanks so much! :D