I’ve been trying to create a simple trick for my horror game, that “transforms” a human into a zombie when you come close enough. Apparently it is really hard to do so, if I don’t even know how to instantiate during runtime.
Basically, I want to destroy one game object, and instantiate another one when the player comes close enough. I hope somebody can help.
I’m a relative newbie, so take this with a grain of salt, but:
make the collider on the “human” circular and large enough to encompass the distance you want the transformation to happen at
put a script with OnCollisionEnter() on the “human”
in OnCollisionEnter, first instantiate the zombie prefab:
var zombiePrefab: Transform; // attach this in the IDE
function OnCollisionEnter(collision: Collision)
{
var zombie: Transform;
print("in OnCollisionEnter");
// attach the prefab to the script or look it up
zombie = Instantiate(this.zombiePrefab, this.transform.position, this.transform.rotation);
// do any other initialization of the zombie object here
then destroy the “human”:
Destroy(this.gameObject);
}
Thanks, but for some reason the "gameObject" doesn't get destroyed, and the "zombiePrefab" doesn't get instantiated. Am I doing something wrong? var zombiePrefab : Transform; function OnCollisionEnter () { zombiePrefab = Instantiate(zombiePrefab, transform.position, transform.rotation); Destroy (gameObject); } Even if I do this: zombiePrefab = Instantiate(zombiePrefab), transform.Find("My Object").transform.position, Quaternion.identify ...still, nothing happens.
Are there any errors in the console? Also, put some print() statements in the OnCollisionEnter() method to ensure it's actually being called. Also, don't overwrite zombiePrefab with the new object. Put it somewhere else.
Just occurred to me,you could try to use the alpha channel of your prefabs:
Keep the way I put above but instead instantiate the zombie with alpha=0 then start to increase that value and decrease the alpha of the human, this way it kinda look like the guy is transforming into the zombie.
When the alpha of the human is 0 destroy the human, when the alpha of the zombie is 1 stop increasing.
OK, thanks. I will try both of your answers now. ;)
@fafase That's a cool idea... if the two game objects are mobile, though, you'd have to keep the motions and animations in sync until the alpha was done.
Also, concerning the motion you can get the transform component of the human and use it for the zombie until the human goes off. Then you cancel and use the own zombie motion. That would solve the motion, for the animation you can synchronize the time of them, there should be a way to get the index of the anim and apply it to the zombie. That would solve your anim problem. Unfortunately, I have not tried that yet so I can't guarantee it would work. It just sounds technically plausible.
Get the range around your fake human(FH) when the player is inside the range destroy the FH and instantiate a zombie using the transform.position of the FH.
var target:Transform; // Drag the player to the slot in the inspector
function Update(){
if(Vector3.Distance(target.position, transform.position)<range;{
var zombie =Instantiate (Zombprefab, pos, Quaternion.identity);
Destroy(gameObject);}}
I am just doing on top of my head so try it and report if something goes wrong or works nice.
I updated a little as I am not sure of the order of instantiation and destroy. In your case there will be both object on the scen for 1 fps but your eye won’t see it.
The order should be ok, and I don't think they'll both be on the scene at the same time: I believe that Update() is called atomically and the zombie won't show up until the next Update(). I wonder which is more efficient: using a collider, or calculating the distance every frame...
First there is an error in that line: if(Vector3.Distance(target.position, transform.position)<range; I added a ")" and I get an error in this line: var zombie =Instantiate (Zombprefab, pos, Quaternion.identity); I don't know what to do
Thanks, but for some reason the "gameObject" doesn't get destroyed, and the "zombiePrefab" doesn't get instantiated. Am I doing something wrong? var zombiePrefab : Transform; function OnCollisionEnter () { zombiePrefab = Instantiate(zombiePrefab, transform.position, transform.rotation); Destroy (gameObject); } Even if I do this: zombiePrefab = Instantiate(zombiePrefab), transform.Find("My Object").transform.position, Quaternion.identify ...still, nothing happens.
– ColdPlutoAre there any errors in the console? Also, put some print() statements in the OnCollisionEnter() method to ensure it's actually being called. Also, don't overwrite zombiePrefab with the new object. Put it somewhere else.
– anon82818710no, I don't get any errors in the console.
– ColdPluto