i have read some tutorials on waypoint to follow my character. i tried on a capsule. but when i tried it on a character(3d model) it seems that it is not working.
here is my capsule ai follow code:
var waypoint:Transform[];
var speed:float=20;
private var cwaypoint:int;
function Update () {
if(cwaypoint<waypoint.length){
var target:Vector3=waypoint[cwaypoint].position;
var moveDirection:Vector3=target-transform.position;
var velocity=rigidbody.velocity;
if(moveDirection.magnitude<1){
cwaypoint++;
}
else{
velocity=moveDirection.normalized*speed;
}
}
rigidbody.velocity=velocity;
transform.LookAt(target);
}
i assume it only works for an object with a rigid body.
Yes, that code will only work on an object with a RigidBody component, since it relies on setting rigidbody.velocity to make the object move. You just need to replace that assignment with the appropriate code to get the object you’re working with to move – for example, if it’s a character, you might be using a CharacterController, in which case you would use its Move() or SimpleMove() function; at the very least, you can use ‘transform.position = transform.position + (velocity * Time.deltaTime);’, but note that you wont get any collision detection or physics simulation with direct movement like that.
Start by looking at the scripts and other components attached to the object you want to move; if you have any code already that moves the object, look at that too and see what kind of movement control it’s using. You can then do similar in your new script.
so, you used old Steamis50 video tutorials i see, hes a nice guy same place where i learned my AI!
Anyway, here you go mate, i converted your code to be usable with any gameObject and any collider (character controler or anything default).
It does not however loop trough waypoints, but you can easly set up bool to do that
So yeah, this is what Laurie meant:
waypointAI.js
var speed : float = 1;
var rotateSpeed : float = 1;
var Rot : Quaternion;
private var cwaypoint:int;
var waypoint:Transform[];
function Update(){
if(cwaypoint<waypoint.length){
var target:Vector3=waypoint[cwaypoint].position;
var moveDirection:Vector3=target-transform.position;
//move towards waypoint
if(moveDirection.magnitude<1){
cwaypoint++;
}
else {
var delta = target - transform.position;
delta.Normalize();
var moveSpeed = speed * Time.deltaTime;
transform.position = transform.position + (delta * moveSpeed);
//Rotate Towards
Rot = Quaternion.LookRotation(target - transform.position,Vector3.up);
transform.rotation = Quaternion.Slerp(transform.rotation, Rot,Time.deltaTime * rotateSpeed);
}
}
}
thanks raymix. but i dont quite understand what you mean by “loop through waypoints.”
EDIT POST:
i have a problem. it stops before reaching the waypoint. how can i change it? i want it to stop if it reaches the same coordinates as the waypoint. thanks.
i have this screenie showing the problem after taking a long walk.
the AI (the one that is slanted) became like that after reaching the waypoint.
no colliding happens.
hi iamchoii:
It does not collide with colliders because it no longer relies on rigid body, thus physics are not used. Truth is, it’s faster on CPU resources this way and for object not to go trough each other you can use magnitude or sqrMagnitude to measure distance, same way you did in code with waypoints. For example, tell script to look for particular object by tag. If distance in magnitude equals or is less than 2 units, make it stop.
As for looping: It means that when you set waypoints, you are using an array to numerate them inside editor. For example, when you launch your game and you have 5 waypoints, it will follow waypoints in this order: 0, 1, 2, 3, 4. That is 5 waypoints in total.When reached waypoint 5 object stops with current script. But you can check if number of current waypoint equals the last destination, you can reset that destination and send object back to first waypoint. And it starts from beginning. That is called looping trough waypoints. Achievable trough something like (lets assume you have 5 waypoints):
// if reached fifth waypoint and distance is less than 1 unit
if (cwaypoint == 4 moveDirection.magnitude<1){
// then reset waypoint back to first
cwaypoint = 0;
}
That being set back to 0 means Vector3 of destination point will be reset to first waypoint and object will move in loop
Also yes you can follow anything other than waypoints. You should finish watching Steamis50 videos, he will show you how to follow waypoints and player or any object when distance is close enough. Should do you for start
You should really look up some beginner scripting tutorial, its all basic math atm, but you cant make this sudden jump into AI without any programming background, its awfully hard (doable, but hard), apart from helping, no one will do scripting for you, unless its pretty technical and you know what you’re doing, just need little push in right direction
thanks for the help. i already understand it. i manage to do the looping. all i need is to reset the waypoint back to 0.
but there is still the physics problem. i have an idea in my mind. but i need your opinion.
my plan is to get the AI very close to the character more likely get them collide. if the physics is disabled here.
maybe i can get it really close like it is collided.
so i manage to get it very close. but when im stepping in an uneven terrain it became (look at the screenshot above) like that.
umm i hope you understand what im trying to do. but hey thanks anyways. the Steamis50 videos are a big help.
yeah that will happen. And that is normal, if you want precision, you should go back to old code and use physics instead. The above code i posted is simply conversation - different way to do it. Since it doesnt use physics its way easier on CPU, but not very good for precise AI.
I would use my posted example for minor AI, like random critter or static AI like turrets. For chararcter AI, go for rigid body definately
If you’d use open space or even flat terrain, this code would work to trick eye too.