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 Brotoss · Oct 28, 2011 at 10:37 AM · movingloopingrespawning

Moving an object forward, killing the object and respawning a new one.

So i have made a extremly simple pice of code, i do not know alot about coding.

 var speed : float = 2.0;



function Update() {

     transform.position += Time.deltaTime * speed * transform.forward;





}

This makes my object move forward from where its at and i can choose what speed it has. How do i make it die after x seconds and then making it respawn? Do i use a forloop or something? I dont know how to write a code for a loop so i need some help.

thanks in advance

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by aldonaletto · Oct 28, 2011 at 10:02 AM

That's not a loop case: you should have a timer variable; set it to 0 at the beginning and increment it each Update, comparing to the end time; when the time is reached, destroy the object and instantiate it at the start position, or just "teleport" it back to the beginning and zero the timer variable to restart the whole thing - something like this:

var speed : float = 2.0; var timer : float = 0; var endTime : float = 5; var initialPos : Vector3;

function Start(){ initialPos = transform.position; }

function Update() {

 transform.position += Time.deltaTime * speed * transform.forward;
 timer += Time.deltaTime;
 if (timer >= endTime){
     // it's easier to just move it to the beginning:
     transform.position = initialPos;
     // reset timer:
     timer = 0;
 }        

} To kill and respawn the object, you should have another script to control the process or do a "clone-and-suicide" thing like this:

    ...
    if (timer >= endTime){
        Instantiate(transform, initialPos, transform.rotation);
        Destroy(gameObject);
    }
}
This would destroy the variables too, but the brand new object would born with timer zeroed, since that's how we're initializing the variable.
The first alternative is preferred, since the second would create and delete memory blocks, what would expend time in the garbage collection, an internal house cleaning operation that recycle the deleted memory.
Comment
Add comment · Show 2 · 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
avatar image Brotoss · Oct 28, 2011 at 11:44 AM 0
Share

Thank you so much! This saved my life :D Love you <3

avatar image Brotoss · Oct 28, 2011 at 11:45 AM 0
Share

I was going to do something like a raycast perhaps, but this was much easyer

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

player not moving after respawn(java script) 1 Answer

Limit Diagonal Speed 2 Answers

DastardlyBanana(moving while jumping. 2 Answers

Rigidbody on a platform 2 Answers

How to make move up text Animimation when Button click? 0 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