The question is answered, right answer was accepted
Can no1 help me???
I have posted this question twice and even with multiple advice, it has not helped.
How do i make ai move between random points(I have this working), but have them move there and not teleport. O have tried lerp and nothing. Help please!!
Script(mainly handling the ai move under if (waypointtimer <= 0):
public class AIBehaviour : MonoBehaviour
{
public NavMeshAgent agent;
public Transform player;
private float DAI;
bool PlayerSeen;
float movespeed;
float timer = 5;
private Vector3 newPos;
public int moveCount;
public bool AImove;
Animator anim;
bool AIpatrol;
float AIhealth;
Vector3 randomPos;
float waypointtimer = 5;
GameObject prefab;
GameObject Waypoint;
// Use this for initialization
void Start()
{
PlayerSeen = false;
movespeed = 1;
moveCount = 0;
AImove = false;
anim = GetComponentInChildren<Animator>();
AIhealth = 100;
newPos = transform.position;
prefab = Resources.Load("Waypoint") as GameObject;
}
// Update is called once per frame
void Update()
{
timer -= Time.deltaTime;
var aix = new Vector3(Random.Range(0, 20), 0, Random.Range(0, 20));
Waypoint = Instantiate(prefab, aix, Quaternion.identity) as GameObject;
if (timer < 0)
{
timer = 5;
}
waypointtimer -= Time.deltaTime;
Vector3 positionA = new Vector3(Random.Range(0, 50), 0, Random.Range(0, 20));
if (waypointtimer <= 0)
{
newPos = positionA;
Vector3 positionB = new Vector3(Random.Range(0, 50), 0, Random.Range(0, 20));
transform.position = Vector3.Lerp(positionA, positionB, Time.deltaTime * movespeed);
waypointtimer = 20;
}
Vector3 direction = player.position - this.transform.position;
float angle = Vector3.Angle(direction, this.transform.forward);
float angleb = -Vector3.Angle(direction, this.transform.forward);
if (Vector3.Distance(player.position, this.transform.position) < 10 && angle < 30 || (Vector3.Distance(player.position, this.transform.position) < 4 && angleb < 0))
{
PlayerSeen = true;
}
/* if (PlayerSeen == false)
{
transform.position = new Vector3(aix, 0.0f, aiz) * (movespeed);
moveCount += 1;
anim.SetBool("Take 001", AIpatrol);
randomPos = transform.position;
}
}
else
{
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 (moveCount == 2)
{
AImove = true;
}
if (moveCount == 2)
{
AImove = false;
}
if (Vector3.Distance(player.position, this.transform.position) > 10 || (Vector3.Distance(player.position, this.transform.position) > 4))
{
PlayerSeen = false;
}
}
}
}
Have people been suggesting various things, that you sort of understand, and you've been cram$$anonymous$$g them all into this? That's always a mess.
$$anonymous$$y advice is to restart with what you know. Something simple that you can test. For example, walking toward one point, which you set in the Inspector. Then think of one think to add - like making that one target point be random. Then test that. $$anonymous$$aybe something someone wrote earlier will help, or not. $$anonymous$$aybe most the stuff in the script you have now will eventually make sense. Just keep adding (and testing) things which you understand.
If just the program$$anonymous$$g part is trouble, it's faster and less frustrating to read a C# book. I (obviously) think $$anonymous$$e at taxesforcatses is the best, but you should be able to get other recommendations here (but not the Unity/Learn section - you wouldn't learn Photoshop from the Unity site, and neither should you learn program$$anonymous$$g.))
Answer by joelunity · Jan 06, 2017 at 07:20 PM
I beleive this code will work for you
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class randomMove : MonoBehaviour { float movespeed = .08f; Vector3 aix = new Vector3(0, 0, 0); void Update () {
if (transform.localPosition != aix)
{
transform.LookAt(aix);
transform.position = Vector3.MoveTowards(transform.position, aix, movespeed);
}
else
{
aix = new Vector3(Random.Range(0, 20), 0, Random.Range(0, 20));
}
}
}