- Home /
respawn not working, transform.position
I have no idea why this code isn't working! it should. I have a character and a on that character I have a respawn.js. below is the code for it. now, it was originally working when I was using the update function as posted below, but when I take that out and put in the OnControllerColliderHit function (also listed below) it doesn't work anymore. you'll see 2 debug.logs in the forceRespawn function. the first debug gives me this:-362.9575 (transform.position.y) the second gives me this: 579.3809 (transform.position.y after the respawn but, my character doesn't actually move at all!). is there something about needing to move the transform in the Update function or what??
static var startingspawnpointx; static var startingspawnpointy; static var startingspawnpointz;
function Awake() { startingspawnpointx = transform.position.x; startingspawnpointy = transform.position.y; startingspawnpointz = transform.position.z; }
function Update() { //check if transform (what this script is connected to) fell off platform and respawn it. if(transform.position.y < -2000) { forceRespawn(); } }
function forceRespawn() { Debug.Log(transform.position.y); transform.position.x = startingspawnpointx; transform.position.y = startingspawnpointy; transform.position.z = startingspawnpointz; Debug.Log(transform.position.y); } function OnControllerColliderHit(hit: ControllerColliderHit) { if (hit.gameObject.name == "Respawn") { Debug.Log("about to"); forceRespawn(); Debug.Log("respawn"); } }
$$anonymous$$y character does the same thing too except even thought I put him 9 meters above the terrain he goes through ONLY AFTER RESPAWN
With Update as posted, it worked, but with the OnControllerColliderHit as posted in s$$anonymous$$d, it did not? You must have confirmed that your forceRespawn is firing and the positions you list are what you expect, but when running, it "doesn't work", right? Since the posted code seems fine, I can only think that it may be something else interfering - Are any other scripts moving the GameObject in question? (in theory it may have worked before because this script's update fired later than the other) Also, have you tried a Debug of the position in the update to confirm the respawn change is staying?
Answer by e-bonneville · Apr 30, 2010 at 01:01 AM
Wow, that's crazy. You're working way too hard. Try this simple code instead:
var SpawnPoint : GameObject;
function Awake() { SpawnPoint.transform.position = transform.position; }
function Update() { if(transform.position.y < -2000) { forceRespawn(); } }
function forceRespawn() { transform.position = SpawnPoint.transform.position; }
function OnControllerColliderHit(hit: ControllerColliderHit) { if (hit.gameObject.name == "Respawn") { forceRespawn(); } }
Just create an empty GameObject for your spawnpoint and drag it onto the open slot in the inspector when you put this script on your character.
awesome idea man but it didn't work either. I land on the "Respawn" platform below my terrain and i just sit there. it doesn't move my "First Person Controller" transform to the "SpawnPoint" I did some trouble shooting and it does indeed go into the if statement and yet I don't move.
There's no need for empty game objects...change "`var SpawnPoint : GameObject`" to "`private var spawnPoint : Vector3`", change "`SpawnPoint.transform.position = transform.position`" to "`spawnPoint = transform.position`", and "`change transform.position = SpawnPoint.transform.position`" to "`transform.position = spawnPoint`". Also it's a good idea to use lowercase for variable names and uppercase for function names.
ok, I did that Eric, good advice to tidy up the code, but the problem still remains. do I have to put it in like the update function? because that one works, but the Respawn platform doesn't work. (the OnControllerColliderHit function doesn't work)
Well, the OnControllerColliderHit won't work unless your collider is not set to be a trigger... is that how you have it? And, @Eric, thanks for the suggestions. I can't fix the code right now, because I'm working, but I'll be sure to do that. I usually do name my variables in camelBack, but I just copied and modified his code.
Are you using a rigidbody for your character? If I remember correctly, you can't use transform
to move your character in this case, and have to use rigidbody.position
in this case.
Your answer
![](https://koobas.hobune.stream/wayback/20220612201536im_/https://answers.unity.com/themes/thub/images/avi.jpg)