Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by BrodiBoy · May 05, 2016 at 06:50 AM · aizombie

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;
         //}
     } 
 }

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image BrodiBoy · May 05, 2016 at 05:27 AM 0
Share

The text above the script where is says public class is part of the script.

avatar image FortisVenaliter BrodiBoy · May 05, 2016 at 07:36 PM 0
Share

Fixed that for you. In the future, be sure to click the code formatting button, or indent the code by 4 spaces.

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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; 
 }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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)?

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image BrodiBoy · May 05, 2016 at 05:45 PM 0
Share

How would i set this code up in C#? Sorryim new at this....reason why im asking

avatar image
0

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image BrodiBoy · May 05, 2016 at 07:54 PM 0
Share

Are u able to put this in C#

avatar image tuncOfGrayLake BrodiBoy · May 05, 2016 at 08:18 PM 0
Share

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);
      }
 }
avatar image BrodiBoy tuncOfGrayLake · May 06, 2016 at 02:33 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Zombie AI tutorial 2 Answers

Zombie A.I path finding help 2 Answers

Need help with javascript AI 1 Answer

Zombie AI running in place but not moving 1 Answer

Zombie AI Lag when checking if it should jump 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges