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

Help fixing AI?

$
0
0
So my AI works pretty well right now, but there are things I want it to do that wont work with the code I have later on. Im am currently moving it towards the player using transform.translate, but I want to switch it to velocity so it wil have collision and i will be able to mak things like knockback. Although, I dont have any real idea how to switch it velocity from here, so I was wondering if anyone could help me out. (I assume its pretty simple but I cant seem to figure it out) Heres my enemy AI script: using System.Collections; using System.Collections.Generic; using UnityEngine; public class GruntAI : MonoBehaviour { public float moveSpeed; public float shootSpeed; private bool inRange; private bool canGen; public float patrolArea; private Transform playerPostion; private Vector2 randPosition; private Rigidbody2D rb; // Use this for initialization void Start () { playerPostion = GameObject.FindGameObjectWithTag("Player").GetComponent(); inRange = false; canGen = true; randPosition = new Vector2(Random.Range(patrolArea * -1, patrolArea), Random.Range(patrolArea * -1, patrolArea)); rb = GetComponent(); } // Update is called once per frame void Update () { if (inRange == true) { transform.position = Vector2.MoveTowards(transform.position, playerPostion.position, moveSpeed * Time.deltaTime); } else { transform.position = Vector2.MoveTowards(transform.position, randPosition, moveSpeed / 2 * Time.deltaTime); } if (canGen == true) { StartCoroutine(generatePosition()); } } private void OnTriggerEnter2D(Collider2D other) { if (other.tag == "Player") { inRange = true; } } private void OnTriggerExit2D(Collider2D other) { if (other.tag == "Player") { inRange = false; } } IEnumerator generatePosition() { canGen = false; randPosition = new Vector2(Random.Range(patrolArea * -1, patrolArea), Random.Range(patrolArea * -1, patrolArea)); yield return new WaitForSeconds(1); canGen = true; } }

Viewing all articles
Browse latest Browse all 1488

Trending Articles



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