Infection script

Hello Everyone,

Ok so here is what I am trying to do: (please keep in mind I am new to scripting so excuse any ignorance on my part)

1: NPCs walk randomly round an environment
2: Infected NPCs are inserted into the environment
3: non - Infected NPCs walk away from the infected
4: Infected NPCs walk towards non infected NPCs
5: When an Infected NPC collides with a non infected that non infected then become infected and adopts that behaviour.

Edit - Point 3: non infected are meant to walk away from infected :slight_smile:

Thats it! At this point I have NPC’s randomly walking around an environment. However I have no idea how to create an infected script.

What im asking? - A script applied to one object (infected), when that object collieds with another object that object then takes on the script of the infected.

Any help you be massively appreciated.

Thanks All in advance

Terry

There are a few techniques for doing hunter/prey scripts.

I would drop “3) Infected NPCs walk away from the infected” - If a non infected was surrounded by infected then they wouldn’t want to move closer to each other.

Take a look at UnitySteer and Boids.

Have you started to do a script?

  1. Search the scripting reference for MoveTowards or Lerp. Have them move towards nearest non-infected.

  2. One way to change between an infected and non-infected, is when they collide, destroy the non-infected, and instantiate an infected at the same location.

Thanks guys, In terms of writing the infect script I have just started. Though I am very much leaning on the job. So far I have been working out how to define an NPC as infected. Then when a collision occurs to check if its a non-infected, if so to destroy and spawn an infected NPC.

You just need a “bool Infected” that you check to see if that NPC should be moving towards/away.

It’s not difficult to get a list of GameObjects in a certain radius to check.

Then check the scripts on them for their Infected variable and use that to steer away or towards.

If an infected collides with a non-infected… make the Infected variable on its script True.

Might want to have them focus on one non-infected (the closest) or they’ll be distracted.

So far I have this script for destroying an infected npc,

I currently have two npcs moving around, the non-infected one has this script attached. The other is called “Infected”.

However when they collide the npc with this script attached does not destroy. If I change the gameObject.name in the script to “Cube” it will destroy when it hits a static cube named “Cube”.

the only difference i can see between a static cube and my ‘infected’ is that i have a character controller attached to the infected. Thats because im using this for the random walker.

Ok so here is my code for destroy and then spawn an infected gameoject. At the moment the script destroys but does not then spawn a new game object. Can anyone see why?

Infected is a game object and prefab neither is spawned :frowning:

I don’t think your “Instantiate” is written correctly. There’s no location or rotation… Do you get any errors in the console? Check out:

static function Instantiate (original : Object, position : Vector3, rotation : Quaternion) : Object

Frankly, tho’, unless your model needs to change from human to mutant… I’d not try to swap game objects, but as written above, either activate/deactivate a script component on the object, or click a boolean in one component from infected == false, to infected == true…

Thanks Guys,

I know have a script that infects game object that are called “infected” if it hits them.

Though im going to tidy it up and try to implement a boolean var for infection. Just need to work out how i perform a check what value the boolean is when a collision occurs.

I do need to destroy the model as the infected will take on a zombie model when infected.

Take a look at collison:

You can get the gameobject with:

You can get component on that gameobject with:

Then you can set the variable in that component.

Thanks Little Angel,
Its great to get some help through these early stages.

So what i think it should be doing:
‘Hit’ is defined as an object that has been hit, then i ‘get component’ which is ‘infected’ (thats a script attached to the game object that is defined by ‘Hit’.

Then create a var called check to reference the script ‘infected’. Then ‘if check= true’ destroy current game object.

The script 'infected is just a simple boolean.

I feel im close with this one, just cant see it from the right angle. At the moment I get no errors, but the current game object is not destroyed.