- Home /
Help fixing AI?
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<Transform>();
inRange = false;
canGen = true;
randPosition = new Vector2(Random.Range(patrolArea * -1, patrolArea), Random.Range(patrolArea * -1, patrolArea));
rb = GetComponent<Rigidbody2D>();
}
// 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;
}
}
Answer by MT369MT · Jun 09, 2018 at 10:31 AM
Hi, try with this:
public float moveSpeed;
public float shootSpeed;
private bool inRange;
public float patrolArea;
private Transform playerPosition;
private Vector2 randPosition;
private Rigidbody2D rb;
// Use this for initialization
void Start()
{
playerPosition = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
inRange = false;
randPosition = new Vector2(Random.Range(patrolArea * -1, patrolArea), Random.Range(patrolArea * -1, patrolArea));
rb = GetComponent<Rigidbody2D>();
InvokeRepeating("generatePosition", 0, 1);
}
// Update is called once per frame
void Update()
{
if (inRange == true)
{
rb.velocity = (playerPosition.position - transform.position).normalized * moveSpeed * Time.deltaTime;
}
else
{
rb.velocity = (new Vector3(randPosition.x, randPosition.y, 0) - transform.position).normalized * moveSpeed * Time.deltaTime;
}
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player")
{
inRange = true;
}
}
private void OnTriggerExit2D(Collider2D other)
{
if (other.tag == "Player")
{
inRange = false;
}
}
public void generatePosition()
{
randPosition = new Vector2(Random.Range(-patrolArea, patrolArea), Random.Range(-patrolArea, patrolArea));
}
If when you press play the enemy doesn't move increase the moveSpeed. I also changed the Coroutine, WaitForSeconds with a InvokeRepeating Function. If you don't need it copy only the part with rb.velocity = ...
Yup, that works! I havent really used an Invoke Repeating yet so thats good to know. Do you have any idea how adding animations to an enemy based on how its moving would work? Thanks a ton though!
How many move animations do you have? One for each direction (up, down, right, left) or its only one and you want to make it facing your direction?
Yeah 2 frames of walking in each direction.
I found here a good answer about it. In the second part of the answer there is the code if you have 4 directions animation and you want to have the closest one (if you walk 45º it will be an Up Animation)
https://answers.unity.com/questions/515880/how-to-play-correct-animation-based-on-direction.html
Your answer
![](https://koobas.hobune.stream/wayback/20220612164418im_/https://answers.unity.com/themes/thub/images/avi.jpg)