- Home /
Question by
Ninita · Feb 10, 2013 at 10:56 PM ·
movementsystemlocomotionbots
How to create a movement system for a bot?
Hello, I'm trying to create routes of travel for some bots in a game I'm creating but I have some doubts on how to move. I've seen some examples of scripts for motion and sometimes use the component "Animator" and sometimes the "CharaterController" and sometimes the "Locomotion". Only that they are unsure how to resolve my case. I have to use all three? : S Please give me any tips. I want to initially position the bot in a certain position and after some time he wants to move to another .. : (
public class at3parnav : MonoBehaviour
{
protected float speed = 0.0f;
public Vector3 posInicial;
protected Vector3 posDestino;
static float lastTime = 0;
protected Animator animator;
protected CharacterController controller;
protected Locomotion locomotion;
protected float[] intervalosTempo = { 5.0f, 7.0f, 13.0f };
private float tempoLimite;
private int cont;
public Vector3 PosInicial { get { return this.posInicial; } }
void Start()
{
animator = GetComponent<Animator>();
controller = GetComponent<CharacterController>();
locomotion = new Locomotion(animator);
switch (UnityEngine.Random.Range(0, 3))
{
case 0: tempoLimite = intervalosTempo[0]; break;
case 1: tempoLimite = intervalosTempo[1]; break;
case 2: tempoLimite = intervalosTempo[2]; break;
}
definePosicaoDestino();
posInicial = posDestino;
transform.position = posDestino;
cont = 0;
lastTime = Time.time;
}
void Update()
{
if (lastTime == tempoLimite)
{
definePosicaoDestino();
lastTime = 0;
}
else
{
if (Vector3.Distance(animator.rootPosition, posDestino) == 0)
{
locomotion.Do(0, 0);
animator.SetBool("Run", false);
}
else
{
Vector3 velocity = Quaternion.Inverse(transform.rotation) * Vector3.ClampMagnitude(transform.position, 0.5f);
locomotion.Do(speed, (Mathf.Atan2(velocity.x, velocity.z) * 180.0f / 3.14159f));
animator.SetBool("Run", true);
}
lastTime = Time.time;
}
}
void OnAnimatorMove ()
{
controller.Move (posDestino);
}
public void definePosicaoDestino()
{
int randomX = UnityEngine.Random.Range(0, 10);
int randomZ = UnityEngine.Random.Range(0, 10);
posDestino.x = randomX;
posDestino.y = 0;
posDestino.z = randomZ;
}
}
Comment