- Home /
How can I call this player transform.position code once, then stop?
Hi all, I am using the following code to move my player to the check point (after death, if the check point is cleared). That works fine, but then the player is stuck at the check point transform position. Any suggestions on how to get my player moving freely again after moving to the check point? Thanks!
void Start (){
if (checkPoint >= 1) {
gameObject.transform.position = currentCheckPoint.transform.position;
}
}
And...
public class CheckPointScript : MonoBehaviour {
void OnCollisionEnter2D(Collision2D collision) {
if (collision.gameObject.tag == "Player"){
MainScript myPlayer = (MainScript) GameObject.Find("Player").GetComponent("MainScript");
myPlayer.currentCheckPoint.transform.position = this.transform.position;
}
}
}
Answer by blueLED · Dec 11, 2014 at 03:08 AM
I think what you need to do is disable the checkpoint after the player has re-spawned. Because right now, it looks like when the player touches the checkpoint he is being constantly reset to the checkpoint position. So just turn off the checkpoint once it's been touched by the player?
Thanks blueLED. I set the game object to false which seems to take care of the constant position reset. I still wanted to have the checkpoint visible, so I put another sprite behind the original checkpoint game object. Add an animation and I should be good to go :)
Answer by Jason-H · Nov 16, 2014 at 10:18 AM
Have you got anything checking whether your character should be able to move or not? Maybe you declare the character as unmovable once they die and you're not changing that back upon respawn?
Thanks Jason H. The player can move, but seems restrained to the location of the checkpoint transform (and only sometimes). I'm not sure what is causing the inconsistent results and therefore how I can go about fixing them. Anybody have any suggestions?
It could be because you have a logic issue where you keep on setting the player position to the ckeckpoint position, so it looks like it has snagged. Put a debug log statement next to any player re-position code and see if that is being called constantly, if so that is what you need to stop/change :)
Thanks for the comment $$anonymous$$rSoad. It does seem to call repeatedly which could very well be causing my problem. Any advice on how to get it to stop calling considering my posted code? And sorry for the delay in my reply...work issues as well :D