- Home /
How to respawn a box once destoyed???
Hi all I wonder if someone can help me with a particular problem that I'm having. The help would be much appreciated. The scenario that I've got would be that I have two boxes. One is following the playable character, whereas the other is following the non-playable character. When the playable character collides with the box following the non-playable character it gets desstroyed. But what I want to do is once it is destroyed respawn it and make it follow the box that is following the playable character.
Here are my scripts so far:
function OnControllerColliderHit (CChit : ControllerColliderHit) { Debug.Log(CChit.gameObject.name); if(CChit.gameObject.name == "Following_NPC") {
Destroy(gameObject.Find("Following_NPC"));
}
}
var speed = 6.0; var jumpSpeed = 8.0; var gravity = 10.0; var rotateSpeed = 1.0; private var moveDirection = Vector3.zero;
function FixedUpdate() {
var controller : CharacterController = GetComponent(CharacterController);
if (controller.isGrounded) {
moveDirection = Vector3(0, 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if(Input.GetButton("Jump")){
moveDirection.y = jumpSpeed;
}
} // Move the controller controller.Move(moveDirection * Time.deltaTime);
// Rotate around y axis transform.Rotate(0, Input.GetAxis ("Horizontal") rotateSpeed, 0); //transform.Translate(Input.GetAxis ("Vertical") speed, 0, 0);
//Apply gravity moveDirection.y -= gravity*Time.deltaTime;
}
function FixedUpdate() { //Assign z axis direction to newly created variable var fwd = transform.TransformDirection(Vector3.forward);
//If not casting ray at the position of character, along the z axis and a meter forwards then if(!Physics.Raycast(transform.position, fwd, 1)) {
//Move 1 meter forwards transform.Translate(0, 0, 1*Time.deltaTime); //And rotate 10 degrees (clockwise) along the y axis transform.Rotate(0, 10*Time.deltaTime, 0);
}
}
var cam_target1 : Transform; //var cam_target2 : Transform; var camRate1; //var CChit : ControllerColliderHit; //camRate variable is the speed at which the camera travels to get to the cube once it has finished moving
//function Start() { //transform.LookAt(cam_target1); //}
function FixedUpdate() { Following_NPC.transform.position); transform.LookAt(cam_target1); camRate1 = 5 * Time.deltaTime; if(Vector3.Distance(cam_target1.position, transform.position) > 2) {
if(transform.position.x > cam_target1.position.x) {
transform.position.x = transform.position.x - camRate1;
}
else if(transform.position.x < cam_target1.position.x) {
transform.position.x = transform.position.x + camRate1;
}
if(transform.position.z > cam_target1.position.z) {
transform.position.z = transform.position.z - camRate1;
}
else if(transform.position.z < cam_target1.position.z) {
transform.position.z = transform.position.z + camRate1;
}
if(transform.position.y > cam_target1.position.y) {
transform.position.y = transform.position.y - camRate1;
}
else if(transform.position.y < cam_target1.position.y) {
transform.position.y = transform.position.y + camRate1;
}
}
}
Format your code so that we can read it. Is that one script or did you post more than one? seperate them "clearly" and you should post relevant code. I just stepped into that right at start in OnControllerColliderHit: Don't use gameObject.Find() it's very slow and you actually have already the reference to this object. Use Destroy(CChit.gameObject); ins$$anonymous$$d. It's still not clear what you want to respawn and why? If you destroy it and respawn it immediately, why not just move it to the new/respawn position.
Answer by FLASHDENMARK · Feb 01, 2011 at 07:59 PM
You want to respawn the box again if it is destroyed?
try something like this:
var destroyed = false;
function Update () { // Destroy the box here. destroyed = true;
if(destroyed) { // Respawn the object again(Instantiate it or something) // and add the code to follow the object you want to follow. } }
Hope this will help you.
Your answer
![](https://koobas.hobune.stream/wayback/20220613102304im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to use a script on multiple gameobjects, then change a variable for one of them, not the other. 3 Answers
Falling off respawn 4 Answers
Destroyable Box 1 Answer
When all objects with a certain tag has been destroyed, load the next level! 3 Answers
Destroy all GameObjects EXCEPT some... 2 Answers