Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Random Binaries · Feb 01, 2011 at 07:39 PM · destroyrespawn

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;
        }
    }

}

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Feb 01, 2011 at 08:54 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges