Virus run…in Unity

This is the first step of a little app done in Unity. The goal is to escape the covid, each time the Player is touch by the virus, it multiplies…

D:\LAURENT\Downloads\WhatsApp Image 2020-11-05 at 21.10.52.jpeg

The labyrinth is created in Sketchup, but could have been created in any 3D software and exported as a 3DS file in Unity.

The site is prepared as Navigation Static

In the navigation panel it is defined as Walkable

And then it is possible to bake the mesh.

As a result, a walkable zone is defined

We will use Third Person Controller – Basic Locomotion FREE

Check that the Tag Player is set.

Create a virus model

Add a Nav Mesh Agent Component

Add a CSharp script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
 
public class Seek : MonoBehaviour
 
{
 
public float Mass = 15;
public float MaxVelocity = 3;
public float MaxForce = 15;
 
private Vector3 velocity;
 
public Transform target;
 
private void Start()
 
{
velocity = Vector3.zero;
}
private void Update()
{
var desiredVelocity = target.transform.position - transform.position;
desiredVelocity = desiredVelocity.normalized * MaxVelocity;
var steering = desiredVelocity - velocity;
steering = Vector3.ClampMagnitude(steering, MaxForce);
steering /= Mass;
velocity = Vector3.ClampMagnitude(velocity + steering, MaxVelocity);
transform.position += velocity * Time.deltaTime;
transform.forward = velocity.normalized;
Debug.DrawRay(transform.position, velocity.normalized * 2, Color.green);
Debug.DrawRay(transform.position, desiredVelocity.normalized * 2, Color.magenta);
}
}

Add the script to the Virus

As target, the Head Top End can be chosen…

Test the application

Now the virus has to multiply;

Add a sphere to the virus

Uncheck the Mesh Renderer

Enlarge the Sphere Collider and ckeck the is Trigger box

Add an Audio Source for an audio feedback, add an audio file

Add the Contamination script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Duplicate : MonoBehaviour
 
{
 
AudioSource _audioSource;
 
public AudioClip _audioClip;
public Transform Spawnpoint;
public GameObject Virus;
public Duplicate(GameObject virus)
 
{
 
Virus = virus;
 
}
void Start()
{
 
_audioSource = GetComponent<AudioSource>();
 
}
void OnTriggerEnter(Collider other)
{
 
if (other.CompareTag("Player"))
 
{
Instantiate(Virus, Spawnpoint.position, Spawnpoint.rotation);
_audioSource.PlayOneShot(_audioClip);
}
 
}
 
}

Once done create a Prefab from the Virus.

The Prefab holds all scripts and heriarchies.

Put Prefabs in the scene and play!!

Laisser un commentaire

Votre adresse de messagerie ne sera pas publiée. Les champs obligatoires sont indiqués avec *

Ce site utilise Akismet pour réduire les indésirables. En savoir plus sur comment les données de vos commentaires sont utilisées.