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

transform.position transports me to the wrong position

Discussion in 'Scripting' started by spartaboy300, Aug 13, 2015.

  1. spartaboy300

    spartaboy300

    Joined:
    Jun 24, 2014
    Posts:
    16
    Okay so when I use transform.position to move my player into a building, it moves me to coordinates that are either wrong or on a different terrain. And I know it isn't because of scale or the parent not being at (0, 0, 0) because all the other trigger collisions in that group work. This one even works if I move it along the x-axis away from the door. But for some reason it just doesn't work in that little area and I have no clue why. And it's not a scripting error because all the others work! Any help is GREATLY appreciated! :)
     
  2. blizzy

    blizzy

    Joined:
    Apr 27, 2014
    Posts:
    775
    So please show us your script?
     
  3. spartaboy300

    spartaboy300

    Joined:
    Jun 24, 2014
    Posts:
    16
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class SceneChange : MonoBehaviour {
    6.  
    7.     public Text pressE;
    8.     public Collider Player;
    9.     public string Scene;
    10.     public bool Trigger;
    11.     public Vector3 doorPosition;
    12.  
    13.     void Start ()
    14.     {
    15.         pressE.text = "";
    16.         Trigger = false;
    17.     }
    18.  
    19.  
    20.     void OnTriggerEnter (Collider Player)
    21.     {
    22.         Trigger = true;
    23.         pressE.text = "Press 'E' to Use";
    24.     }
    25.  
    26.     void Update ()
    27.     {
    28.         if (Trigger == true)
    29.         {
    30.             if (Input.GetKeyDown ("e"))
    31.             {
    32.                 Player.transform.position = (doorPosition);
    33.             }
    34.         }
    35.     }
    36.  
    37.     void OnTriggerExit (Collider Player)
    38.     {
    39.         Trigger = false;
    40.         pressE.text = "";
    41.     }
    42. }
    43.  
    Here ya go
     
  4. Dreamteck

    Dreamteck

    Joined:
    Feb 12, 2015
    Posts:
    336
    Maybe your door transform's pivot is offsetted or doorPosition doesn't get set properly. Try having a Transform doorTransform variable instead of a Vector3 doorPosition and then set the position as follows:
    Player.transform.position = doorTransform.position;
    If that doesn't work, try: Player.transform.parent = doorTransform; Player.transform.localPosition = Vector3.zero; and see where this get's the player.
     
  5. Carvuh

    Carvuh

    Joined:
    Mar 25, 2013
    Posts:
    25
    Try what Dreamteck said, and if that doest work just try a simple,

    Code (csharp):
    1.  
    2. // This pops up in the console window.
    3. Debug.Log(doorPosition);
    4.  
    In the script. Just run it by the numbers in your scene view. If you door position does not match, that is due to the origins of your door gameobject (It's where you see the colored axis's show up in the scene view) not matching up correctly.

    To make a quick fix for THAT. You could create an empty GameObject, call it something like, DoorOrigins, and then just use the position of that (once you have parented it to the door, and placed it in a good position before parenting it) to make sure the player gets updated.