Quantcast
Channel: Questions in topic: "enemy"
Viewing all articles
Browse latest Browse all 1488

Enemy Patrol - Random Walk

$
0
0
I have my AI script already and its fairly simple, I wanted to add a code where the enemy can look at its own position and every couple seconds walk a little bit in a random direction.. I just dont know how to do it, way points and paths wont really work for my game as im going to be respawning and killing these mobs so I wanted to make it simple! THANKS IN ADVANCE 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) { animation.Play("idle"); } if(distance < 10) { 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"); } } }

Viewing all articles
Browse latest Browse all 1488

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>