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
1
Question by convictcartel · Dec 27, 2013 at 07:55 PM · airandommonodevelop

Enemy AI move random.

Im looking for a way to make the enemies on my map move randomly around if the player isnt in distance.. heres my AI script. Please and thank you :]

 using UnityEngine;
 using System.Collections;
 
 public class EnemyAI : MonoBehaviour {
     public Transform target;
     public int moveSpeed;
     public int rotationSpeed;
     
     private Transform myTransform;
     
     void Awake() {
         myTransform = transform;
     }
     
     // Use this for initialization
     void Start () {
         GameObject go = GameObject.FindGameObjectWithTag("Player");
         
         target = go.transform;
         
     }
     
     // Update is called once per frame
     void Update () {
         
         float distance = Vector3.Distance(target.transform.position, transform.position);
         
         Debug.Log(distance);
         if(distance < 10 && distance > 2) {
         Debug.DrawLine(target.position, myTransform.position, Color.red);
         
             //look at target/rotate
             myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);
         
             //move towards target
             myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
             animation.Play("walk");
 
     }
 
         if(distance < 2) {
 
             animation.Play("attack");
                     }
 
         if(distance > 10) {
             
             animation.Play("idle");
         }
 
     }
 }
 
Comment
Add comment · Show 1
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 Meater6 · Dec 27, 2013 at 08:46 PM 0
Share

Also, you really should accept correct answers, and upvote them if they were helpful. It's rude not too, and if you don't, the whole unity answers system starts to break down. :/

If you're new to the site, watch this.

1 Reply

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

Answer by Meater6 · Dec 27, 2013 at 08:44 PM

Hello convictcartel!

I'm not that good in C#, so I can't write you a proper script for it to work, but I do know how to do this.

First off, you should be familiar with the Random class.

I'm unsure whether you want to teleport your enemies to random locations, or you simply want them to move to a random point nearby.

The later is fairly simple with your AI setup, and I assume that is what you want. Have a new variable called targetPos (or etc.), and make it normally equal to "target.position". When the player is far enough from the enemy, set targetPos like so:

 //Note: this makes them move in a random SQUARE, not a circle. //A circle would require an angular-radius system.
 
 public float width = 5; //Your square width
 float rx = Random.Range(-1,1);
 float rz = Random.Range(-1,1);
 
 targetPos = Vector3(transform.position.x+(rx*width), someYValue, transform.position.z+(rx*width));

This code is untested. Not only that, I'm not great in C#. Have your enemy move to targetPos instead of target.position.

If the enemy will always be far out of view when moving randomly (and traveling doesn't matter), it will be much more optimal to teleport it. Doing so will vary greatly on the type of game you are making, so I will not post it here.

I bid you good luck, and I hope this helps!

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

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

19 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

Related Questions

Random Running and Follow at Certain Distance 0 Answers

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

Random Movement Problem 1 Answer

AI pathfinding waypoints 1 Answer

random.range not working 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