Home unity3d How to make Respawn in an unity of a specific object in...

# How to make Respawn in an unity of a specific object in its original point after time?

Author

Date

Category

My project is similar to the popular game Hole.IO, the location is filled with different objects that I initially arrange on the stage (in the future it will be, let’s say, the city). And my character, like a black hole, these objects eat: underground there is a plane, when a collision with which the object is deactivated and the player is counted with glasses, after which it grows.
I implemented this, but now I got up, it would seem simple, but an extremely incomprehensible task: how to make it so that after the excitement, the object appears at the original point after time?
I did it like this: at the time of deactivation (or deactivation), the object is cloned (underground) and then its copy appears on top.
Code:

``````if (collision.gameobject.tag == "Plants")
{
Yrot = Instantiate (Collision.GameObject, Collision.GameObject.transform.position, Quaternion.identity) AS GAMEOBJECT; // Copy the object
Yrot.transform.position = new vector3 (Collision.GameObject.transform.position.POSITION.X, 1.25F, Collision.GameObject.transform.position.z); // Make it appear on y above, on earth
// Collision.GameObject.SetActive (FALSE); // deactivate the object and its copy to activate it through time
}
``````

This is a fragment from testing by TEG.
As a result of this code: after the destruction of the original object, it instantly appears its copy but (!!!) in the coordinates where the original was destroyed. Those. As long as, let’s say, the tree fell through a black hole, it could change its coordinates from the initial and then a copy of the tree will also be interpreted along them (new).
I need some particular binding, but to tie each object (which is more than 200 on the map) is not rational.
There was a thought about to save objects immediately after the destruction in a state of deactivation, and after the time they would activate them, as if they just appeared now. But I did not figure it out with the meters, I tried through Karutin. How is my task solve? If possible, tell me the code, for I only learn, this is my first game. Thank!

As I understand it, you need to repaire an object at its point, when it loaded the first time . But you gave little data, tell me how you interact with a tree or house for example. And how did you get them to respapass.

I have an idea about this. In my version, you need to not destroy the object eaten by the player, and for example, to hide it (for the beginner will go think). Then you stand on every object to hang the script (with a class of course), where there will be a variable vector3 startpos and float timeedead . When starting, for example, in Awake or Start, you in the variable startpos write the coordinates of the object.

``````startpos = new vector3 (transform.position.x, transform.position.y, transform.position.z);
``````

Then you need a dead function, it is called when an object is eaten. In it, in the variable TimeDead you write the time from the start of the game Plus time which will not appear the object (for this can be a separate variable for convenience), that is:

``````timedead = time.time + 5;
``````

where 5 is the most time that the object will not appear .
And then you must check, time.time & gt; = TimeDead, if yes (more), then the object can be re-showing.

Here is an example of the function of death and checking on the resolution of spawn (I am writing without an IDE, so soy if there are syntax errors):

``````Public Void Awake ()
{
// Start Games
STARTPOS = new vector3 (transform.position.x, transform.position.y, transform.position.z);
}
{
// death
GameObject.SetActive (False);
}
Public Void Update ()
{
if (Time.time & gt; = TimeDead)
{
// show the object and move it to the coordinates of Startpos.
GameObject.SetActive (True);
transform.position = startpos;
}
}
``````

Well, it seems to say everything. Fresh a little and get gorgeous. If there are any questions – please ask – I’m not a master to explain.

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.