- Home /
Object will not return to start position after player respawns
Hello, I'm stuck on a part of my code for a object that fly's towards you after you trigger it to. The object has collider on it that is used for a trigger and a child object on it that uses my Respawn Script to reset the player when it touches the object that too has a collider. My question is that I can't make it return to the start position after it made it to it's target position. I know that isDead is not being triggered because there's no condition to tell it to be true if something happens but I can't figure out how I would write it out in code to make it know when the play hits the collider with the Respawn Script on it makes the object go back to it's start position.
Thank You to anyone that helps out.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class fireWorks : MonoBehaviour {
public Transform target;//Go to this position
public Transform returnTarget;//Go back to start position
public float speed;//speed it travels at
public bool setOff;
public bool isDead;
public Respawn myRespawn;
void Start(){
GameObject.FindWithTag("Firework").GetComponent<Respawn>();
isDead = false;
}
void OnTriggerEnter2D(Collider2D other){
if(other.gameObject.CompareTag("Player")){
setOff = true;
}
}
void Update(){
if(setOff){
float step = speed * Time.deltaTime;
transform.position = Vector3.MoveTowards(transform.position, target.position, step);
}
if(isDead == true){//if dead move back.
setOff = false;
float beep = speed * Time.deltaTime;
transform.position = Vector3.MoveTowards(transform.position, returnTarget.position, beep);
}
}
}
Hi Andrew, can you clarify your question a little bit? It's hard to understand which part you are stuck on. Are you trying to find a Respawn script that is attached to the Player?
The Respawn Script isn't attached to the player character it's attached to a child object on the moving object. Sorry for not being more clear.
If this help here is my Respawn Script.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Respawn : $$anonymous$$onoBehaviour {
[SerializeField]
private Transform player;
[SerializeField]
private Transform respawnPoint;
void OnTriggerEnter2D(Collider2D other)
{
if(other.gameObject.tag == "Player"){
Debug.Log("WOR$$anonymous$$!!!");
player.transform.position = respawnPoint.transform.position;
//When placing this on a object BOX COLLIDER must be made IS TRIGGER.
}
}
}
Answer by NoDumbQuestion · Jun 19, 2018 at 02:04 AM
How did you attach myRespawn
?
Because in script you find that object but didnt attach it with anything
public Respawn myRespawn;
void Start(){
GameObject.FindWithTag("Firework").GetComponent<Respawn>();
isDead = false;
}
Did you mean this?
myRespawn = GameObject.FindWithTag("Firework").GetComponent<Respawn>();