Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
-1
Question by Zitoox · Nov 02, 2016 at 04:48 PM · movementrigidbodyrespawndetecting

Respawn objects

Hi! I have some cups in my game, and i wanted to respawn them. There are 10 cups exactly tagged as cup, each one has rigidbody and they all are children of an object named DRINKS. When the player move towards the cups or shoot them they are thrown by physics to a random location. The problem is that it is so funny that most people will want to do it a lot of times xD

So i have been searching, trying to learn how to make a script that in every frame detects the location of the cups, and respawn all of them if one of them or more have been moved.

I need to make it in the Update function, so it will search for movement in every frame, and i need to detect the objects. The problem is that i don't know how to detect if they have been moved and how to respawn them all if one or more moved from it's original position.

Here's a script that i have made for now, it's pretty basic bcs most of the code i don't know what to write.

 #pragma strict
 
 //No variables, there are too much cups to do such thing.
 
 function Update () //searches every frame.
 {
     GameObject.FindGameObjectWithTag("Cup"); //This will find all the cups (i guess).
 
     // Detect if they have moved??
     //Respawn them??
     
 }

I have read some pages that might have the info that i need but i still didn't get it. Here are the pages:

https://docs.unity3d.com/ScriptReference/GameObject.FindGameObjectsWithTag.html http://answers.unity3d.com/questions/374391/respawn-object.html http://answers.unity3d.com/questions/26856/check-object-for-motion.html

Can someone help me? I can't express how it would help me. It would be a big favour.

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 bubzy · Nov 02, 2016 at 05:25 PM

ok heres a brute force method, im sure theres a clever way of doing it.

 using UnityEngine;
 using System.Collections;
 
 public class cupMove : MonoBehaviour {
 
     // Use this for initialization
     public GameObject[] respawnCups;
     public Vector3[] originalPos;
     public bool[] moved;
     int arrayLength;
     public int maxMoved = 3;
     int numMoved = 0;
     void Start () {
     
             respawnCups = GameObject.FindGameObjectsWithTag("cup");
             int count = 0;
         arrayLength = respawnCups.Length;
         originalPos = new Vector3[arrayLength];
         moved = new bool[arrayLength];
 
     foreach (GameObject pos in respawnCups)
         {
             originalPos[count] = pos.transform.position;
             count++;
         }
     for (int i = 0; i < arrayLength; i++)
         {
             moved[i] = false;
         }
     }
 
     // Update is called once per frame
     void Update()
     {
         for (int i = 0; i < arrayLength; i++)
         {
             if (originalPos[i] != respawnCups[i].transform.position)
             {
                 if (!moved[i])
                 {
                     Debug.Log("Moved");
                     moved[i] = true;
                     numMoved++;
                 }
             }
         }
         if(numMoved > maxMoved)
         {
             for(int i = 0; i < arrayLength; i++)
             {
                 respawnCups[i].transform.position = originalPos[i];
                 moved[i] = false;
             }
             numMoved = 0;
         }
     }
         
         
 }

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 Zitoox · Nov 02, 2016 at 07:08 PM -1
Share

I specified the exact location of each cup, but still it gives me the "move" dialog even if they are in their original position and didn't move at all. Also, they don't respawn, when i shoot them they move a bit and then start glitching and rotating indefinetely ins$$anonymous$$d of falling in the ground and then respawn. I wanted them to stay a little in the ground before respawning and just reappear at the same position.

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

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

Problem with rigidbody 2 Answers

Rigidbody going through Colliders help! 1 Answer

Help converting a character controller script to rigidbody 1 Answer

Determine the direction an object is actually moving? 0 Answers

Player movement help! 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