Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
1
Question by danny_appel · Mar 16 at 06:26 PM · positionrandom.rangepatrol

Can someone help with the logic behind this?

Hi all,


I'm trying to move a character randomly around his starters position, however it doesnt seem to work and I don't understand why it doesnt. Because I believe, it should work?


I have this on him

 float xPos = spawnLocation.x + Random.Range(spawnLocation.x - 10, spawnLocation.x + 10);
 float zPos = spawnLocation.z + Random.Range(spawnLocation.z - 10, spawnLocation.z + 10);
 targetLocation = new Vector3(xPos, gameObject.transform.position.y, zPos);
 myAgent.SetDestination(targetLocation);


So in my head, if he spawns on 0,0,0 then his area of 'patrol' should be max 10,0,10 & -10,0,-10 right? But he goes waaay beyond that and always goes to the same direction once it walks. He goes from 0,0,0 to 33,0,54 for example, way behind his bounderies.


The rest of the code shouldnt be importent as this is all what it needs to move him.


Can someone explain the logic behind this? Am I not seeing something?


 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.AI;
 
 public class EnemyBehaviour : MonoBehaviour
 {
     Animator myAnim;
     CapsuleCollider myCollider;
 
     public GameObject[] regularZombieLooks;
     public GameObject[] bigZombieLooks;
     public GameObject[] rangedZombieLooks;
 
     public Transform target;
     public PlayerShooting playerTarget;
     private Transform myTransform;
     private Vector3 targetLocation;
     private Vector3 spawnLocation;
     public NavMeshAgent myAgent;
     public Transform mouthPoint;
     public GameObject goo;
 
     public bool regular, big, ranged, rangedAttacked = false, meleeAttacked = false, roleChanged = false;
     public bool isChasing = true;
 
     public float health = 100;
 
     public int moveSpeed;
     public int rotateSpeed;
     public float RangedAttackDistance;
     public float MeleeAttackDistance;
     public float RangedAttackCooldownTimer;
     public float MeleeAttackCooldownTimer;
     public float PlayerAggroRange;
     public float ShootingAggroRange;
 
     private int zombieRole = 1;
 
     private void Awake()
     {
         myTransform = transform;
         spawnLocation = transform.position;
     }
 
     void Start()
     {
         SpawnLooksDependingOnRole();
         CheckingRoleIfNotAttacking();
 
         myAnim = GetComponentInChildren<Animator>();
         myCollider = GetComponent<CapsuleCollider>();
         myAgent = GetComponent<NavMeshAgent>();
 
         GameObject go = GameObject.FindGameObjectWithTag("Player");
         target = go.transform;
         playerTarget = go.GetComponent<PlayerShooting>();
     }
 
 
     void Update()
     {
         AnimationBehaviour();
     }
 
     private void AnimationBehaviour()
     {
         Debug.Log(roleChanged);
         
         // Death
         if (health <= 0)
         {
             myAnim.SetTrigger("death");
             myCollider.enabled = false;
             myAgent.isStopped = true;
             isChasing = false;
             Destroy(this.gameObject, 1.5f);
             return;
         }
         
 
 
         // Idle
         if(!isChasing && zombieRole == 1)
         {
             if(roleChanged == true)
             {
                 CheckingRoleIfNotAttacking();
                 roleChanged = false;
             }
 
             myAgent.isStopped = true;
             myAnim.SetBool("chasing", false);
 
             float distaggro = Vector3.Distance(transform.position, target.transform.position);
             if (distaggro <= PlayerAggroRange)
             {
                 // Player is close enough to be chased.
                 isChasing = true;
             }
 
             float distshooting = Vector3.Distance(transform.position, target.transform.position);
             if (distshooting <= ShootingAggroRange && playerTarget.playerIsShooting == true)
             {
                 // Player is shooting nearby and will be chased.
                 isChasing = true;
             }
         }
 
         // Roaming
         if(!isChasing && zombieRole == 2)
         {
             if(roleChanged == true)
             {
                 CheckingRoleIfNotAttacking();
                 roleChanged = false;
 
                 float xPos = spawnLocation.x + Random.Range(spawnLocation.x - 10, spawnLocation.x + 10);
                 float zPos = spawnLocation.z + Random.Range(spawnLocation.z - 10, spawnLocation.z + 10);
                 targetLocation = new Vector3(xPos, gameObject.transform.position.y, zPos);
                 myAgent.SetDestination(targetLocation);
 
 
 
 
                 myAgent.isStopped = false;
                 myAgent.speed = 1f;
                 myAnim.SetBool("chasing", true);
             }
 
             float distaggro = Vector3.Distance(transform.position, target.transform.position);
             if (distaggro <= PlayerAggroRange)
             {
                 // Player is close enough to be chased.
                 isChasing = true;
             }
 
             float distshooting = Vector3.Distance(transform.position, target.transform.position);
             if (distshooting <= ShootingAggroRange && playerTarget.playerIsShooting == true)
             {
                 // Player is shooting nearby and will be chased.
                 isChasing = true;
             }
         }
 
 
 
         // Following the player to attack
         if (isChasing)
         {
             myAgent.destination = target.position;
             myAnim.SetBool("chasing", true);
         }
 
         // Ranged Attack behaviour
         if (ranged && isChasing)
         {
             myAgent.speed = 3f;
 
             float dist = Vector3.Distance(transform.position, target.transform.position);
             if (dist <= RangedAttackDistance)
             {
                 myAgent.isStopped = true;
                 myAnim.SetBool("chasing", false);
                 AimingTowardsPlayer();
 
                 if (rangedAttacked == false)
                 {
                     myAnim.SetTrigger("attack");
                     rangedAttacked = true; GameObject fireGoo = Instantiate(goo, mouthPoint.position, mouthPoint.rotation);
                     fireGoo.GetComponent<Rigidbody>().velocity = mouthPoint.up * 10f;
                     Destroy(fireGoo, 3f);
                     StartCoroutine(RangedAttackCooldown());
                 }
                 return;
             }
 
             if(dist > RangedAttackDistance)
             {
                 myAgent.isStopped = false;
                 myAgent.destination = target.position;
                 myAnim.SetBool("chasing", true);
             }
         }
 
         // Regular Attack behaviour
         if(regular && isChasing)
         {
             myAgent.speed = 3.5f;
 
             float dist = Vector3.Distance(transform.position, target.transform.position);
             if (dist <= MeleeAttackDistance)
             {
                 myAgent.isStopped = true;
                 myAnim.SetBool("chasing", false);
                 AimingTowardsPlayer();
 
                 if (meleeAttacked == false)
                 {
                     myAnim.SetTrigger("attack");
                     meleeAttacked = true;
                     StartCoroutine(MeleeAttackCooldown());
                 }
                 return;
             }
 
             if (dist > MeleeAttackDistance)
             {
                 myAgent.isStopped = false;
                 myAgent.destination = target.position;
                 myAnim.SetBool("chasing", true);
             }
         }
 
         // Big Attack behaviour
         if(big && isChasing)
         {
             myAgent.speed = 2f;
 
             float dist = Vector3.Distance(transform.position, target.transform.position);
             if (dist <= MeleeAttackDistance)
             {
                 myAgent.isStopped = true;
                 myAnim.SetBool("chasing", false);
                 AimingTowardsPlayer();
 
                 if (meleeAttacked == false)
                 {
                     myAnim.SetTrigger("attack");
                     meleeAttacked = true;
                     StartCoroutine(MeleeAttackCooldown());
                 }
                 return;
             }
 
             if (dist > MeleeAttackDistance)
             {
                 myAgent.isStopped = false;
                 myAgent.destination = target.position;
                 myAnim.SetBool("chasing", true);
             }
         }
     }
 
 
 
 
 
 
     private void AimingTowardsPlayer()
     {
         // Calculate the direction
         var direction = target.position - transform.position;
 
         // Ignore hight difference
         direction.y = 0;
 
         // Make the transform look in the direction
         transform.forward = direction;
     }
 
     private void SpawnLooksDependingOnRole()
     {
         if (regular)
         {
             var regularlook = Random.Range(0, regularZombieLooks.Length);
             regularZombieLooks[regularlook].SetActive(true);
         }
 
         if (big)
         {
             var biglook = Random.Range(0, bigZombieLooks.Length);
             bigZombieLooks[biglook].SetActive(true);
         }
 
         if (ranged)
         {
             var rangedlook = Random.Range(0, rangedZombieLooks.Length);
             rangedZombieLooks[rangedlook].SetActive(true);
         }
     }
 
     private void CheckingRoleIfNotAttacking()
     {
         StartCoroutine(RoleChangeCooldown());
     }
 
     IEnumerator RangedAttackCooldown()
     {
         yield return new WaitForSeconds(RangedAttackCooldownTimer);
         rangedAttacked = false;
     }
 
     IEnumerator MeleeAttackCooldown()
     {
         yield return new WaitForSeconds(MeleeAttackCooldownTimer);
         meleeAttacked = false;
     }
 
     IEnumerator RoleChangeCooldown()
     {
 
         yield return new WaitForSeconds(10);
         zombieRole = Random.Range(1, 3);
         Debug.Log(zombieRole);
         roleChanged = true;
     }
 
 
     private void OnTriggerEnter(Collider collision)
     {
         if (collision.gameObject.CompareTag("Bullet"))
         {
             health -= collision.gameObject.GetComponent<BulletDamage>().totalDamage;
             Destroy(collision.gameObject);
         }
     }
 }


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 Narc0t1CYM · Mar 16 at 06:36 PM 0
Share

I think you should add the rest of the code as something else might be changing the values earlier

avatar image danny_appel · Mar 16 at 09:56 PM 0
Share

(code moved)

1 Reply

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

Answer by rh_galaxy · Mar 16 at 06:49 PM

How often do you run this to set the destination? Because each time you run it it has the chance to move further away, you have no real boundaries. Also you add spawnLocation.x one time more than you want, it only works as you intend when spawnLocation.x == 0

If you want boundaries you can just do

 float xPos = Random.Range(-10.0f, 10.0f);

Or if you want it to move more and more perhaps

 float xPos = spawnLocation.x + Random.Range(-10.0f, 10.0f);
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 Narc0t1CYM · Mar 17 at 10:24 AM 0
Share

I agree with your answer. He was taking a Random.Range from spawnLocation.x -/+10 - + 10 instead of taking Random.Range from -10 - +10

avatar image Bunny83 · Mar 17 at 01:38 PM 1
Share

Right. Though there's nothing wrong in using Random.Range(spawnLocation.x - 10, spawnLocation.x + 10) however the result would be the worldspace xPosition. So adding that do the spawnLocation makes no sense. This was the actual issue. So you can do either

 float xPos = Random.Range(spawnLocation.x - 10, spawnLocation.x + 10);
 float zPos = Random.Range(spawnLocation.z - 10, spawnLocation.z + 10);

or

 float xPos = spawnLocation.x + Random.Range( -10f, 10f);
 float zPos = spawnLocation.z + Random.Range( -10f, 10f);

which both give the same result.

avatar image danny_appel · Mar 17 at 02:17 PM 1
Share

Thanks guys! That did indeed fix it! A good nights sleep also helps alot! Thanks again.

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

163 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Generate a Random number between two ranges 6 Answers

My question is almost fully answered(AI help) 0 Answers

Instantiate randomly from array 1 Answer

Patrol enemy going back to origine after reaching a Waypoint 1 Answer

Camera rotation around player while following. 6 Answers


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