Hi, ya guys. Spent all day yesterday figuring this out, no luck!
Learning some AI programming here, following Steamis50’s old tutorials (thx man you rock!), but i stopped half way when the thing hit me and was on my own after that… so…
I am using Gizmos as waypoints, so they are only rendered in Editor, and Using Capsule object to move around them.
Waypoints are defined as arrays.
And when magnitude of distance of player is lower than 5, Capsule is following player, and when player runs out of range, capsule goes back to waypoints. Simple
What works:
If AI is put directly into scene with scripts attached as well as waypoints (Hierarchy tree). This way it works flawlessly
I built SPAWN button. Spawn button sends message to messagee to do spawn function. And so it does, works as intended, using instantiate
What does not work:
For spawn to work, capsule have to be converted to a prefab. This way i can no longer drag&drop static gizmos from scene to Transform of waypoint arrays, it won’t let me anymore.
So what i did, was made waypoint gizmos prefabs too. Gizmos still appear on screen perfectly normal, and i can now drag them onto script’s definded transform fields.
Problem is, once capsule is spawned, it does not see neither static or prefab waypoints, and all spawned capsules just goes into one specific undefined position and starts jiggling there as mad. If player walks up in range at spawn, they follows player, and out of range they goes to that spot again. If spawned without player in range, they totaly ignore player afterwards.
What i did for debug is added Debug.Log(“target changed”); right after currentWaypoint++; and once object was spawned, console was spammed by “Target Changed”. So clearly, it ignores my waypoint prefabs, but static waypoints are impossible to drag&drop onto scipt’s transform field, which is plain stupid
Workaround would be hard-coding waypoints and names, but i want to use arrays and transform, so the values can be changed and drag&dropped on the run.
Hell, even player can not be drag&dropped onto Scripted Prefab’s Target’s Transform field. So i am using code to find object tagged player. This does not work for arrays, cannot convert error comes up
At the moment i am even spawning waypoints on awake, still no juice, can’t see them
Any idea?
here’s the code:
var waypoint : Transform[];
var speed : float = 20;
var currentWaypoint : int;
var loop : boolean = true;
var player : Transform;
function Awake(){
waypoint[0] = transform;
if(!player){
player = GameObject.FindWithTag("Player").transform;
}
}
function Update () {
if(currentWaypoint < waypoint.length){
var target : Vector3 = waypoint[currentWaypoint].position;
var moveDirection : Vector3 = target - transform.position;
var distFromPlayer : Vector3 = player.position - transform.position;
var velocity = rigidbody.velocity;
if(moveDirection.magnitude < 1){
currentWaypoint++;
}
}
else if(distFromPlayer.magnitude < 5){
velocity = Vector3.zero;
velocity = (player.position - transform.position).normalized * speed;
target = player.position;
if(distFromPlayer.magnitude < 1){
velocity = Vector3.zero;
}
else{
velocity = moveDirection.normalized * speed;
}
}
else{
if(loop){
currentWaypoint=0;
}
else{
velocity = Vector3.zero;
}
}
rigidbody.velocity = velocity;
transform.LookAt(target);
}