- Home /
My question is almost fully answered(AI help)
I asked before about my ai script I had and got improvements. Those improvements were fine for the time being, but now i want to move forward with my AI and this is preventing me from doing so. The answer I got when i first asked about having my ai randomly patrol was to use Vector3.MoveTowards, I was a little skeptical because I remember reading that this only moves in a straight line. But i used it and it worked a little bit. But now when my ai character gets to the point it wanted to go, it moves(thankfully it does not teleport) to a bunch of other points about 5 feet from the original point. How do I make my ai take some time when it gets to a destination, and then move like 30 feet from the new point it has reached. Help please
My script:
using UnityEngine;
using System.Collections;
public class AIBehaviour : MonoBehaviour
{
public Transform player;
bool PlayerSeen;
float movespeed;
float timer = 5;
public int moveCount;
public bool AImove;
Animator anim;
float AIhealth;
Vector3 currentTransformedPos;
// Use this for initialization
void Start()
{
PlayerSeen = false;
movespeed = .08f;
moveCount = 0;
AImove = false;
anim = GetComponentInChildren<Animator>();
AIhealth = 100;
}
// Update is called once per frame
void Update()
{
timer -= Time.deltaTime;
Vector3 direction = player.position - this.transform.position;
float angle = Vector3.Angle(direction, this.transform.forward);
float angleb = -Vector3.Angle(direction, this.transform.forward);
if (PlayerSeen == false)
{
transform.LookAt(new Vector3(Random.Range(10, 30), 0, Random.Range(10, 30)));
transform.position = Vector3.MoveTowards(transform.position, new Vector3(Random.Range(10,30), 0, 0), movespeed);
moveCount = 1;
currentTransformedPos = transform.position;
}
if (currentTransformedPos == transform.position && (PlayerSeen == false))
{
transform.LookAt(new Vector3(Random.Range(10, 30), 0, Random.Range(10, 30)));
transform.position = Vector3.MoveTowards(transform.position, new Vector3(Random.Range(10, 30), 0, 0), movespeed);
moveCount = 1;
currentTransformedPos = transform.position;
}
//Player Position
if (Vector3.Distance(player.position, this.transform.position) < 10 && angle < 30 || (Vector3.Distance(player.position, this.transform.position) < 4 && angleb < 0))
{
direction.y = 0;
this.transform.rotation = Quaternion.Slerp(this.transform.rotation,
Quaternion.LookRotation(direction), 0.1f);
}
if (direction.magnitude > 5)
{
this.transform.Translate(0, 0, 0.3f);
}
if (Vector3.Distance(player.position, this.transform.position) < 10 && angle < 30 || (Vector3.Distance(player.position, this.transform.position) < 4 && angleb < 0))
{
PlayerSeen = true;
}
if (Vector3.Distance(player.position, this.transform.position) > 10 || (Vector3.Distance(player.position, this.transform.position) > 4))
{
PlayerSeen = false;
}
{
if (moveCount == 1)
{
AImove = true;
}
if (moveCount == 0)
{
AImove = false;
}
}
}
}