- Home /
Question by
MasterLiam175 · May 14, 2020 at 12:57 PM ·
ai problems
my ai wont move
hi, I'm having some trouble getting my ai to move. it will move without a collider but I need one to activate a script. anything will help. thanks in advance.
public float Range = 10f;
public Transform Target;
bool detected = false;
Vector2 direction;
public float moveSpeed = 10f;
private void Start()
{
}
private void OnCollisionEnter2D(Collision2D coll)
{
Destroy(this.gameObject);
}
private void FixedUpdate()
{
Vector2 targetpos = Target.position;
direction = targetpos - (Vector2)transform.position;
RaycastHit2D rayInfo = Physics2D.Raycast(transform.position, direction, Range);
if (rayInfo)
{
if (rayInfo.collider.gameObject.tag == "Player")
{
if (detected == false)
{
detected = true;
}
}
}
if (detected == true)
{
transform.up = direction;
transform.Translate(transform.up * moveSpeed * Time.deltaTime);
Invoke("die", 3);
}
}
public void die()
{
Destroy(this.gameObject);
}
private void OnDrawGizmosSelected()
{
Gizmos.DrawWireSphere(transform.position, Range);
}
} ,hi, I have a problem with getting my ai to have a collider and use this script. I am completely lost so anything will help. thanks in advance.
using UnityEngine;
public class go : MonoBehaviour { public float Range = 10f;
public Transform Target;
bool detected = false;
Vector2 direction;
public float moveSpeed = 10f;
private void Start()
{
}
private void OnCollisionEnter2D(Collision2D coll)
{
Destroy(this.gameObject);
}
private void FixedUpdate()
{
Vector2 targetpos = Target.position;
direction = targetpos - (Vector2)transform.position;
RaycastHit2D rayInfo = Physics2D.Raycast(transform.position, direction, Range);
if (rayInfo)
{
if (rayInfo.collider.gameObject.tag == "Player")
{
if (detected == false)
{
detected = true;
}
}
}
if (detected == true)
{
transform.up = direction;
transform.Translate(transform.up * moveSpeed * Time.deltaTime);
Invoke("die", 3);
}
}
public void die()
{
Destroy(this.gameObject);
}
private void OnDrawGizmosSelected()
{
Gizmos.DrawWireSphere(transform.position, Range);
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612234203im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Changing AI's target in run time 1 Answer
Enemy AI problem 2 Answers
Properties of Artificail Intellegence in Unity Game Engine 0 Answers
Find turning radius with torque 0 Answers