- Home /
Respawn Point respawning far away
I am working on a marble raceway game where the player can roll around and if his life gets to zero, he respawns. the respawn script basicly sends the spawner gameobject to the latest collision between the player and a respawn point, so when the player dies, the last respawn point that he hit is the one that he starts at. but when I test it, the player respaws roughly 90 units away from the spawn point. My respawn Manager code looks like this :
var marbleLocomosion : Transform;
var marbleHealth : MarbleScript; var marbleCameraTransform : SmoothFollow;
function Awake() {
} function Respawn() { var clone : Transform; Destroy(marbleLocomosion); clone = Instantiate(marbleLocomosion, transform.position, transform.rotation); marbleLocomosion.position = transform.position; marbleHealth.health = 100; marbleCameraTransform.target = marbleLocomosion; }
and my respawn point script looks like this:
var CurrentPosition : Vector3;
var Manager : Transform;
var particleAnimator : ParticleAnimator;
private var manager : RespawnManager;
var isActive : boolean = false;
function Awake() { manager = FindObjectOfType(RespawnManager); } function Update () { CurrentPosition.x = transform.position.x; CurrentPosition.y = transform.position.y; CurrentPosition.z = transform.position.z; if(Manager.transform.position == CurrentPosition) { isActive = true; } else { isActive = false; } if(isActive == true) { particleAnimator.doesAnimateColor = true; } if(isActive == false) { particleAnimator.doesAnimateColor = false; } } function OnTriggerEnter() { Manager.transform.position.x = CurrentPosition.x; Manager.transform.position.y = CurrentPosition.y; Manager.transform.position.z = CurrentPosition.z; }
Answer by Jessy · Jan 28, 2011 at 07:20 PM
Like Jason B, I see nothing wrong with your code, other than the fact that it is really verbose! Here's the exact same thing without a bazillion lines!!! :-O
var marbleLocomosion : Transform; var marbleHealth : MarbleScript; var marbleCameraTransform : SmoothFollow;
function Respawn() { Destroy(marbleLocomosion); marbleLocomosion = Instantiate(marbleLocomosion, transform.position, transform.rotation) as Transform; marbleHealth.health = 100; marbleCameraTransform.target = marbleLocomosion; }
.
var particleAnimator : ParticleAnimator;
private var managerTransform : Transform;
function Awake() { managerTransform = (FindObjectOfType(RespawnManager) as Component).transform; }
function Update () { particleAnimator.doesAnimateColor = managerTransform.position == transform.position; }
function OnTriggerEnter () { managerTransform.position = transform.position; }
Don't name something ...Script, though. It's a script, we know that. Marble, not MarbleScript. That whole variable is bad practice. This is better:
var marble : Marble;
Answer by Jason B · Jan 28, 2011 at 06:53 PM
Have you tried testing multiple respawn points? Do you always respawn at the same relative offset?
I don't see anything in your code that's telling your spawn points to have any sort of offset. So that leads me to believe your spawn points may be parented to something. Are they?
If so, your spawn points are using their own local coordinates and not world coordinates to display their location. For instance, for a child object, 0, 0, 0 isn't the center of the world, it's the center of whatever it's parented to. So getting its position will return 0, 0, 0, and then sending you there will instead be like sending you back to the center of the scene rather than to the spawn point since your character is (assuming) not parented to anything so moving its position will move it in world coordinates.
Anyways, I'll try not to blather too much on this point if this isn't even what the issue is. :)
But if your respawn points are parented to anything, one (of a few) solutions could be to child your character to the respawn point parent, then set its position, then unparent it (a quick and dirty fix, but a fix nonetheless!).
Thanks, but it didn't work. I am still respawning far a away and none of the respawn points are parented or parents to anything. I also put a box under the respawned marble, and I found that none of the components have been added except for those refrenced(the marble script and the smooth follow). I think that it is continuously instantiating and destroying at the position of the marble, not the respawn points. I go to the far away place ( about -86 from the respawn point) and respawn and destroy forever. HELP!! I need to finish this game very soon!!
I just figured out the multple respawn problem, so when I continuously went through the damage, I went to a far closer place on one of the respawn points(the one that sent me far away) and on the other one, slightly left. there seems to be some anonymous line of code that sets my position, but I can't seem to find it. if you need to see the marble script, please just ask and i will post it in an answer.
Is it necessary to destroy the marble only to reinstantiate it? Have you tried just moving it and resetting its health? Anyways, the reason your marble is instantiated as a component-less object is because according to your script, marbleLocomosion is nothing but a transform. You'll need to rewrite that variable as a GameObject, and link it to a prefab from inside the inspector.
Anyways, I don't know how you're handling respawn points, but how often you check health shouldn't cause any problems. All I would do is basically have the points be trigger boxes, and when you hit them, they set some sort of spawn position variable. And when you die, you move position to that variable (or instantiate to it). There shouldn't be any possible way for it to go wrong. I guess I'd have to see more code, because this should be fairly straightforward and I'm not sure why any problems even exist. :(
Your answer
![](https://koobas.hobune.stream/wayback/20220613101553im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Instantiate GUI text 1 Answer
Terrain with lots of objects 1 Answer
cannon script is not working, no errors. 1 Answer
object count cap? 1 Answer