Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 wwewef · Mar 02, 2015 at 03:08 AM · 3dairandomenemymove

Move Enemy randomly in a range. 3D

Hi: Im working on a FPS mixed with some survival in 3D. I have an animated model that represents my enemy. I can make him move towards me if I am in range. If he gets closer enough he stops moving(and animating). Now Im trying to achieve a Wandering behaviour.

My idea goes like this: The enemy starts in the start position. He checks if the player is in range, if true then FollowPlayer() if false then MoveAround(), this means that he calculate a random position in a certain range of his actual position, Slerp to it and then move to that random position. After reaching the random position he should wait X seconds until the next move . The MoveAround() method I have is a Coroutine but I cant make it work. Here is the script: using UnityEngine; using System.Collections;

 public class EnemyAI : MonoBehaviour {
 
     public float rotationSpeed;
     public float moveSpeed;
     public float maxSpeed;
     public float minRange;
     public float maxRange;
     public float moveRange;
     Animator anim;
     CharacterController controller;
 
     // Use this for initialization
     void Awake (){
         anim = GetComponent<Animator> ();
         controller = GetComponent<CharacterController> ();
     }
     void Start () {
     }
     
     // Update is called once per frame
     void FixedUpdate () {
         FollowPlayer ();
         StartCoroutine(MoveAround ());
     }
     void FollowPlayer(){
         Vector3 playerPos = GameObject.Find ("Player").transform.position;
         Vector3 lookDir = playerPos - transform.position;
         Vector3 moveDir = lookDir;// * moveSpeed;
         moveDir *= Time.fixedDeltaTime;
 
         if((Vector3.Distance(transform.position, playerPos) <= maxRange) && (Vector3.Distance(transform.position, playerPos) > minRange) ){
             Vector3 previous = transform.position;
             transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation (lookDir), rotationSpeed * Time.fixedDeltaTime);
             controller.Move(moveDir);
             float velocity = ((transform.position - previous).magnitude) / Time.fixedDeltaTime;
         //    Debug.Log("velocidad: " + velocity);
             previous = transform.position;
             anim.SetFloat ("speed", velocity);
         }
         if(Vector3.Distance(transform.position, playerPos) < minRange){
             controller.Move(Vector3.zero);
             Debug.LogWarning("hey");
             anim.SetFloat("speed",controller.velocity.magnitude);
         }
     }
     IEnumerator MoveAround(){
         Vector3 playerPos = GameObject.Find ("Player").transform.position;
         Vector3 randomPos = Random.onUnitSphere * moveRange;
         randomPos = new Vector3 (randomPos.x + transform.position.x, transform.position.y, randomPos.z + transform.position.z);
         Vector3 lookDir = randomPos - transform.position;
         Vector3 moveDir = lookDir;
         moveDir *= Time.fixedDeltaTime;
         Debug.Log ("Random Pos: " + randomPos);
         Debug.Log ("Look Dir: " + lookDir);
         Debug.Log ("Move Dir: " + moveDir);
 
         if(Vector3.Distance(transform.position, playerPos) > maxRange){
             Debug.Log("Moving the enemy");
             Vector3 previous = transform.position;
             transform.rotation  = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation (lookDir), rotationSpeed * Time.fixedDeltaTime);
         controller.Move(moveDir);
         float velocity = ((transform.position - previous).magnitude) / Time.fixedDeltaTime;
     //    Debug.Log("velocidad: " + velocity);
         previous = transform.position;
         anim.SetFloat ("speed", velocity);
         Debug.Log("Enemy moved");
         yield return new WaitForSeconds(2f);
         Debug.Log("esperando primeros 2 segundos");
     }
     Debug.Log ("he llegado al destino");
     yield return new WaitForSeconds(2f);
     Debug.Log ("COroutine final");
 }

 void OnDrawGizmosSelected(){
     Gizmos.color = Color.green;
     Gizmos.DrawWireSphere (transform.position, maxRange);
     Gizmos.DrawWireSphere (transform.position, minRange);
 }

}

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 mattyman174 · Mar 02, 2015 at 04:50 AM 2
Share
  // Update is called once per frame
 void FixedUpdate () {

You should not be using FixedUpdate for this type of behaviour. Just stick to Update.

avatar image H3lls7rik3r · Mar 03, 2015 at 08:34 AM 0
Share

Is the Coroutine starting?

If it is, until which line of code the coroutine works?

0 Replies

· Add your reply
  • Sort: 

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Transform.translate change 1 Answer

Monster doesn't move 0 Answers

How To Make Enemy Move Around 0 Answers

Make AI walk around randomly until Player is Seen. (C#) 2 Answers

How do I animate my enemy randomly with time? 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