- Home /
How to get a Zombie to Chase Player at a certain distance?
`I am trying to develop a zombie game where the zombie only chases the player at a certain distance. For C#, what would i have to do to set his up? C# only please, Thank You.
using UnityEngine;
using System.Collections;
public class Zombie : MonoBehaviour
{
Transform player; // Reference to the player's position.
//PlayerHealth playerHealth; // Reference to the player's health.
//EnemyHealth enemyHealth; // Reference to this enemy's health.
NavMeshAgent nav; // Reference to the nav mesh agent.
void Awake ()
{
player = GameObject.FindGameObjectWithTag ("Player").transform;
//playerHealth = player.GetComponent <PlayerHealth> ();
//enemyHealth = GetComponent <EnemyHealth> ();
nav = GetComponent <NavMeshAgent> ();
}
void Update ()
{
// If the enemy and the player have health left...
//if(enemyHealth.currentHealth > 0 && playerHealth.currentHealth > 0)
//{
// ... set the destination of the nav mesh agent to the player.
nav.SetDestination (player.position);
//}
// Otherwise...
//else
//{
// ... disable the nav mesh agent.
//nav.enabled = false;
//}
}
}
The text above the script where is says public class is part of the script.
Fixed that for you. In the future, be sure to click the code formatting button, or indent the code by 4 spaces.
Answer by Free_Radical · May 06, 2016 at 06:04 AM
Here's a very simplistic solution. I decided to break it up a bit so it would be more "step-by-step" than highly efficient.
public GameObject MyTarget; // Reference to whatever you want the zombie to react to
float DistanceToAttack = 5.0f; // Distance you want the zombie to begin attacking from
void Update()
{
// Calculate distance to target
float DistanceToTarget = CalculateDistance(MyTarget);
// If at the DistanceToAttack or less, then attack
if (DistanceToTarget <= DistanceToAttack)
{
Attack();
}
// Otherwise do nothing/idle
else
{
Idle();
{
}
// Simple implementation of Pythagorean formula to calculate distance
float CalculateDistance(GameObject MyTarget)
{
// Grab references to positions of player and zombie as a Vector3
Vector3 MyPosition = transform.position;
Vector3 TargetPosition = MyTarget.transform.position;
// Assuming you're working with x and z values here in 3d space, ignoring height.
// If not, use the y values instead.
float X = Math.Abs(MyPosition.x - TargetPosition.x);
float Z = Math.Abs(MyPosition.z - TargetPosition.z);
float D = Mathf.Sqrt(Math.Pow(X, 2) + Math.Pow(Y, 2));
return D;
}
Answer by Jaxogon · May 05, 2016 at 04:35 PM
Have you thought about using Vector3.Distance? (When the Distance is in the range that you want, then trigger the chase)?
How would i set this code up in C#? Sorryim new at this....reason why im asking
Answer by tuncOfGrayLake · May 05, 2016 at 07:14 PM
If you want zombies to follow your player at a certain distance then here's an example pseudo-code for you.
If the zombies are too close, slow down their follow velocity.
Else if the zombies are at an optimum distance, keep the follow velocity unchanged.
Else if the zombies are too far away, speed up the follow velocity.
You can see this link here to see two ways of making your zombies follow the player. One of them uses the MoveTowards and the other uses the Lerp function.
I tried something slightly different. Could you give this a shot and see if it works for you?
//A modified version of SmoothFollow2 from http://wiki.unity3d.com/index.php/SmoothFollow2
using UnityEngine;
using System.Collections;
public class ZombieFollow : $$anonymous$$onoBehaviour
{
public Transform playerTr;
private Transform zombieTr;
public float distance = 3.0f;
public float height = 3.0f;
public float damping = 5.0f;
private Vector3 followPosition;
void Awake()
{
zombieTr = this.transform;
}
void Update ()
{
Follow();
}
private void Follow()
{
//Transforms the position x, y, z from local space to world space.
followPosition = playerTr.TransformPoint(0, height, -distance);
//Lerp the zombie towards the followPosition.
zombieTr.position = Vector3.Lerp (zombieTr.position, followPosition, Time.deltaTime * damping);
}
}
It seems to be staying away from the player at whatever distance it is at. I'm trying to have it where the zombie is idle until the player comes close enough to the zombie and then the zombie chases the player.
Your answer
Follow this Question
Related Questions
Zombie AI tutorial 2 Answers
Zombie A.I path finding help 2 Answers
Need help with javascript AI 1 Answer