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 kyogretcg · Mar 23, 2013 at 03:59 AM · objectsrespawncollisions

How do I make and object move then respawn at it's start point and move back again in an infinite loop?

Hi I'm new to scripting and was wondering if I could get a little help with getting an object to move forward then when it hits a plane to disappear and respawn at it's starting location and move forward again. I have multiple objects on multiple levels that need this attached. Any help is appreciated aswell as any guides to learning how to script.

Comment
Add comment · Show 2
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 robertbu · Mar 23, 2013 at 07:25 AM 0
Share

There are multiple ways in Unity to move things and detect things. You need to decide how you are going to move it and how it interacts with the rest of the environment before anyone can give you an informed answer.

avatar image kyogretcg · Mar 24, 2013 at 09:11 PM 0
Share

Well I just want it to move forward on the X axis and when it hits a plane to respawn at its starting point

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by BlackWingsCorp · Mar 26, 2013 at 12:50 PM

Man I feel like I'm the dumbest smart ass in the community for not thinking about this (bear with me writing it takes wayyy longer then applying): 1) put your barrel where you want it to be at it starting point 2) move the barrel to where you want it to be in case it didn't hit a plane and check the position's x axis' value in the inspector 3) return the barrel to it's starting position 4) select the barrel and open window --> animation then click on the blank box next to "GameObject" and click create new clip 5) name the animation and save it 6) select position.x and click "Add Curves" then right-click on the brighter grey area under the time (just under the time you feel is best) then add Keyframe PS: 1:00 is 1 second not minute just click, hold and drag the keyframe to edit the time 7)enter the x's new value then close the window 8)click on the animation in the project view and change the wrap mode to loop (inspector view) 9)to make the barrel go back to its original place on contact with any gameobject simply attach to it a new js script and just write these lines (ps: deselect all old scripts you won't need them):

 function OnCollisionEnter(col : Collision){
     if(col.gameObject){
     this.transform.position.x = //original x's value eg: 23.7;
     }
     }

Now play it: your barrel should move from the starting to the finishing point in a constant loop then thanks to our new script if any gameobject collides with the barrel it will return to it's starting point. Hope this 1 solves the prob. PS: as I said delete the empty gameobject and the scripts I wrote earlier.

Comment
Add comment · Show 1 · 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 dicraft · Apr 21, 2013 at 08:50 AM 0
Share

You, sir, are a diety to me! Thanks a lot!

avatar image
1

Answer by BlackWingsCorp · Mar 23, 2013 at 11:59 AM

ok I found the bug. I kinda "tricked" it to do what I want (the beauty of scripting lol) so you're gonna get an error on the console saying "destroying assets is not permitted to avoid data loss" don't worry cause this "error" tricks the engine on doing exactly what we want it to do. So here's the script you should add to the empty gameobject :

 var speed : int = 2000;
 var spawnedObject : Rigidbody;
 var spawnNumb : int;
 
 function Start(){
 spawnNumb = 0;
 }
                 
 function Update () {
 Launch();
 }
 
 function Launch(){
 //this spawns the object after 2 seconds
 yield new WaitForSeconds(2);
 if(spawnNumb == 0){
 var newspawn : Rigidbody = Instantiate(spawnedObject, transform.position, transform.rotation);
 spawnNumb ++;
 //moves the object according to the given direction and speed
 newspawn.AddForce(Vector3.forward * speed);
 yield new WaitForSeconds(2);
 Destroy(spawnedObject.gameObject);
 spawnNumb --;
 }
 }


Now add this to the barrel prefab:

 function OnCollisionEnter(col : Collision){
 if(col.gameObject){
 Destroy(this.gameObject);
 }
 }

Now an instance of the barrel should move forward according the the position/rotation of the empty gameobject will disappear if it hits any gameobject or disappear after 2 seconds in a constant loop. Hope it helps

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
avatar image
0

Answer by kyogretcg · Mar 24, 2013 at 06:07 PM

i keep getting an error saying "unknown identifier: newspawn" how do I fix this. The name of the object I want to move is "Barrel"

Comment
Add comment · Show 7 · 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 BlackWingsCorp · Mar 24, 2013 at 06:28 PM 0
Share

It worked well with me. Did you assign the barrel object to the spawnedObject variable in the inspector? And did you attach the script to an empty gameobject?

avatar image kyogretcg · Mar 24, 2013 at 09:10 PM 0
Share

well thats the problem is when I save the script it give me the error saying it doesn't recognize newspawn and so I can't do anything without that being fixed. I made the empty gameobject and attached my barrel to it but the script is giving me an error. and I'm not seeing the spawnedObject variable in the inspector. Do you mean in the script?

avatar image BlackWingsCorp · Mar 25, 2013 at 01:17 PM 0
Share

I just edited my answer. Let me know how it works

avatar image kyogretcg · Mar 26, 2013 at 12:04 AM 0
Share

The error is gone but now the barrel stays in place then disappears after two seconds and never comes back. If you'd like I can upload a copy of my game level to mediafire so you can see exactly what I'm trying to accomplish.

avatar image kyogretcg · Mar 27, 2013 at 02:25 AM 0
Share

thanks man this helped so much I really appreciate the help and one more thing if I may. Do you know any good tutorials or websites where I can learn to script? Thanks again.

Show more comments

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

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Game Object is not working properly 0 Answers

Very Basic collision with uneven things 1 Answer

Collsions - Objects just passing through each other 1 Answer

Multiple Collisions Within Different Objects 0 Answers

How to change scenes without reloading them. 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