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

Random Object Movement

$
0
0
Sort of like random paths around them.. I want them to be walking within somewhere near them so they dont walk all over the map just little steps yunno... THANK YOU so much for future help hhaha using UnityEngine; using System.Collections; public class EnemyAI : MonoBehaviour { public Transform target; public int moveSpeed; public int rotationSpeed; public float width = 5; float rx = Random.Range(-1,1); float rz = Random.Range(-1,1); 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"); } } }

Viewing all articles
Browse latest Browse all 1488

Trending Articles



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