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.
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;
}
}
}
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
Follow this Question
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