- Home /
Transform Position Not Moving Object
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;
}
Answer by smoggach · Aug 20, 2014 at 04:47 AM
You're teleporting the player object to pointB then immediately back to pointA then your system gets out of sync. try else if instead of if in the second case inside OnCollisionStay
else if (thePlayerPosition == pointB) {
In the first case, after you teleport him to pointB, you update GridMove.playerLastPosition but fail to update thePlayerLastPosition which gets checked immediately after.
Thanks. I clean up the code and fixed the movement by transfor$$anonymous$$g the collision object. Works like a charm now. Here is the complete code in someone ever needs it.
using UnityEngine;
using System.Collections;
public class Teleport : $$anonymous$$onoBehaviour {
public Vector3 pointA;
public Vector3 pointB;
private Vector3 thePlayerPosition;
private Vector3 thePlayerLastPosition;
void Update () {
thePlayerPosition = GridPlayerController.playerPosition;
thePlayerLastPosition = GridPlayerController.playerlastPosition;
}
void OnCollisionStay ( Collision thePlayerCollision ) {
if (thePlayerPosition == pointA) {
if (thePlayerLastPosition != pointB) {
TeleportToPosition( thePlayerCollision, pointB, pointA );
} else {
return;
}
} else if (thePlayerPosition == pointB) {
if (thePlayerLastPosition != pointA) {
TeleportToPosition( thePlayerCollision, pointA, pointB );
} else {
return;
}
}
}
void TeleportToPosition ( Collision thePlayerCollision, Vector3 thePlayerDestination, Vector3 playerlastPosition ) {
if(this.GetComponent<AudioSource>() != null) {
audio.Play();
}
thePlayerCollision.transform.position = thePlayerDestination;
GridPlayerController.playerlastPosition = playerlastPosition;
}
}
So, what I figured out is that you have to disable the object you are trying to change position on, set the position and than enable it.
Player.SetActive(false); Player.transform.position = new Vector3(PositionX, PositionY, PositionZ); Player.SetActive(true);
This might be because during gameplay the player or the object you are trying to move is in static mode.
Chirag Shah
Your answer
![](https://koobas.hobune.stream/wayback/20220613161027im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Teleporting Player to a point using box colliders 1 Answer
Transform.Position Help 3 Answers
Moving gameobject relative to resolution 1 Answer
Teleport object to other objects position 2 Answers
Teleportation Loop issue 1 Answer