1
\$\begingroup\$

So I'm basically trying to get my character to "use" a set of stairs by teleporting from one stairway door to another. Each stairway door will be paired with one other stairway door.

Here's the code, I feel like it should be working...my Debug.Logs are even outputting the correct information. Also FYI, I have the Transform doorPairedWith as public so that I can just drag and drop the stairway door that each one is supposed to be paired with.

 using UnityEngine;

 public class Stairs : MonoBehaviour {

 Player player;

 public Transform doorPairedWith;

 Vector3 doorPairedWithPosition;
 Vector3 playerPosition;

 BoxCollider2D playerCollider;
 BoxCollider2D stairsTriggerCollider;

 bool playerIsNearStairs = false;

 void Start()
 {
     player = Player.instance;

     playerPosition = player.transform.position;
     doorPairedWithPosition = doorPairedWith.position;

     playerCollider = player.GetComponent<BoxCollider2D>();
     stairsTriggerCollider = GetComponent<BoxCollider2D>();
 }

 void Update()
 {
     UseStairs();
 }

 void UseStairs()
 {
     if (playerIsNearStairs == true)
     {
         if (Input.GetKeyDown(KeyCode.E))
         {
             Debug.Log("Attempting to use stairway door named: " + doorPairedWith);
             playerPosition = doorPairedWithPosition;
         }
     }
 }

 void OnTriggerEnter2D(Collider2D collision)
 {
     if (collision.gameObject.tag == "Player")
     {
         playerIsNearStairs = true;
         Debug.Log("Player is near stairs: " + playerIsNearStairs);
     }
 }

 void OnTriggerExit2D(Collider2D collision)
 {
     if (collision.gameObject.tag == "Player")
     {
         playerIsNearStairs = false;
         Debug.Log("Player is near stairs: " + playerIsNearStairs);
     }
 }
 }
\$\endgroup\$
2
\$\begingroup\$

Vector3s are not reference type. The actual GameObject will not move until you modify that object's transform.

UseStairs sets playerPosition, but never sets player.transform.position to the new data.

your code should look something like this.

void UseStairs()
{
     if (playerIsNearStairs)
     {
         if (Input.GetKeyDown(KeyCode.E))
         {
             Debug.Log("Attempting to use stairway door named: " + doorPairedWith);
             player.transform.position = doorPairedWithPosition;
         }
     }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.