- Home /
What am I doing wrong with my AI?
I'm trying to make an EnemyAI for a spider that makes the Enemy follow the player regardless of sight, and can climb over objects and terrain to get to the player (I don't have the climbing portion yet) so I'm using transform.position to move the Enemy, but he passes through all objects and terrain except for the ground. What am I doing wrong, here's my AI code.
using UnityEngine;
using System.Collections;
public class EnemyAI : MonoBehaviour
{
public Transform target;
public int moveSpeed;
public int rotationSpeed;
public int maxdistance;
private float EnemyGravity;
public Animator anim;
private Transform myTransform;
void Awake()
{
myTransform = transform;
}
void Start()
{
EnemyGravity = 0;
anim = GetComponent<Animator>();
GameObject go = GameObject.FindGameObjectWithTag("Player");
target = go.transform;
maxdistance = 2;
}
void Update()
{
Debug.DrawLine(target.position, myTransform.position, Color.red);
Debug.Log(CollisionFlags.Below);
gravCheck(1);
Rigidbody rb = GetComponent<Rigidbody>();
myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);
if (Vector3.Distance(target.position, myTransform.position) > maxdistance)
{
//Move towards target
anim.Play("walk");
rb.velocity = (myTransform.forward * moveSpeed) + (myTransform.up * EnemyGravity);
}
}
private void gravCheck(float gravity)
{
Rigidbody rb = GetComponent<Rigidbody>();
RaycastHit hit;
if (!Physics.Raycast(transform.position, Vector3.down, out hit, 1.5f))
{
EnemyGravity -= 0.7f;
if (EnemyGravity < -15)
EnemyGravity += 0.7f;
}
if (Physics.Raycast(transform.position, Vector3.down, out hit, 1.5f))
{
EnemyGravity = 0;
}
Vector3 move = new Vector3(0, EnemyGravity, 0);
rb.velocity = move;
}
}
Answer by ericbegue · Jun 06, 2016 at 05:11 PM
Don't modify the position of the object directly unless you are handling a kinematic object. Also, any physics related processing has to be done in FixedUpdate and not in Update. It's ok to to set the velocity instead of using AddForce, but only in the case you want to apply some short impulse that result into a controlled change in velocity.
Since you are designing a spider, I would suggest to manage your own gravity force and apply it in the direction of the wall/ground normal so that the spider "stick" to the climbed surface. Just apply a downward gravity when the spider is in mid-air to make it fall to the ground.
About designing AIs, If you intend to make a complex AI, I suggest to learn about Behaviour Tree. It's the best AI tool I know so far to describe behaviours.
Have a look at Panda BT (www.pandabehaviour.com), it's a script-based Behaviour Tree framework.
If you have any question about using this tool, you're welcome on this thread.
Your answer
Follow this Question
Related Questions
AI code for enemy car to follow the player car 0 Answers
Multiple objects follow the object in front 4 Answers
Move an object on collision 1 Answer
Network Trasnform Interpolation Factor 2 Answers
Random direction with Mouse Click... 2 Answers