- Home /
Monster AI
Hey guys, this is the code:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class MonsterAI : MonoBehaviour {
public int i;
public List<Vector3> Targets = new List<Vector3>();
RaycastHit hit;
public bool on;
public float TurnSpeed;
public float Speed;
CharacterController controller;
// Use this for initialization
void Start () {
controller = GetComponent<CharacterController>();
gameObject.SendMessage("Old");
}
void OnTriggerEnter(){
on = true;
i = Targets.Count-1;
}
// Update is called once per frame
void Update () {
if(on){
if(transform.position == Targets[i]){
i++;
}
// find the target direction:
Vector3 dir = Targets[i] - transform.position;
dir.y = 0; // make direction strictly horizontal
dir.Normalize(); // normalize it
// turn gradually to the target each frame:
transform.forward = Vector3.Slerp(transform.forward, dir, TurnSpeed * Time.deltaTime);
Vector3 forward = transform.TransformDirection(Vector3.forward);
controller.SimpleMove(forward * Speed);
}
}
public IEnumerator Old(){
yield return new WaitForSeconds(2);
Targets.Add(GameObject.FindWithTag("Player").transform.position);
gameObject.SendMessage("Old");
}
}
I want the monster to follow the steps of the player but it reaches near the first point and stucks, i dont know why.
Ins$$anonymous$$d of using bool on, you can use setactive to chose if the script is working or not.
What I like to do and I suggest to you is to use a collider to see if the point is contained. You need less accuracy with the movement to reach the point. Also, if you have a lot of points very close to each other in many different orientation, it will prevent the gameObject to "go crazy all around" and turning on itself.
Thank you, the problem it the end was the ground. I straigthened it and it works fine. But many maps are not straight so i need to fix it. But i cant change the collider material on the character controller. Any suggestions?
Your answer
Follow this Question
Related Questions
AI Monster Problem 3 Answers
Wandering AI Monster 2 Answers
How to make a giant walking monster 1 Answer
How to check if a character hasn't changed its position in the last x seconds? 1 Answer
Enemy AI scripting help 1 Answer