I’m having a problem changing an object position. I have a teleport system that when the player step in he’s moved to another position. The code is working fine, but won’t change the player object position.
Here is my code:
using UnityEngine;
using System.Collections;
public class Teleport : MonoBehaviour {
public Vector3 pointA;
public Vector3 pointB;
private GameObject thePlayer;
private Vector3 thePlayerPosition;
private Vector3 thePlayerLastPosition;
void Start() {
thePlayer = GameObject.FindGameObjectWithTag("Player");
}
void Update () {
thePlayerPosition = GridMove.playerPosition;
thePlayerLastPosition = GridMove.playerlastPosition;
}
void OnCollisionStay ( Collision other ) {
print("Player Has Arrived");
if (thePlayerPosition == pointA) {
print("Player is on Teleport A at: " + thePlayerPosition);
if (thePlayerLastPosition == pointB) {
return;
} else {
TeleportToPosition( pointB );
GridMove.playerlastPosition = pointA;
}
}
if (thePlayerPosition == pointB) {
print("Player is on Teleport B at: " + thePlayerPosition);
if (thePlayerLastPosition != pointA) {
TeleportToPosition( pointA );
GridMove.playerlastPosition = pointB;
} else {
return;
}
}
}
void TeleportToPosition ( Vector3 thePlayerDestination ) {
print("Teleport Player to Position: " + thePlayerDestination);
thePlayer.transform.position = thePlayerDestination;
}
}
Here is my console output.
Player Has Arrived
Player is on Teleport A at: (2.0, 0.0, 2.0)
Teleport Player to Position: (7.0, 0.0, -2.0)
If i change for example just to test, the player to “this” the block that I step in moves to the position.
void TeleportToPosition ( Vector3 thePlayerDestination ) {
print("Teleport Player to Position: " + thePlayerDestination);
this.transform.position = thePlayerDestination;
}