Fairly new to Unity/C#. I’m doing an FPS. I’ve gotten all the animations down, but when it comes to actually having the enemy move, I’m having some issues. I’d like a physics based locomotion so the creature doesn’t just clip through walls. I’m aware of Rigidbody,AddForce and CharacterController.Move but I have no idea how to “phrase” them given my current setup or what values to put where.
I’ve tried the AddForce page, but again, I’m unsure how to phrase it in a way, so that when the Walking animation starts, and the enemy has spotted the player, that it moves toward them.
public class WastrelChase : MonoBehaviour {
public Transform player;
static Animator anim;
// Use this for initialization
void Start ()
{
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update () {
if (Vector3.Distance(player.position, this.transform.position) < 5)
{
Vector3 direction = player.position - this.transform.position;
direction.y = 0;
this.transform.rotation = Quaternion.Slerp(this.transform.rotation,
Quaternion.LookRotation(direction), 0.1f);
anim.SetBool("IsIdle", false);
if (direction.magnitude > .8)
{
anim.SetBool("IsWalking", true);
anim.SetBool("IsStriking", false);
}
else
{
anim.SetBool("IsStriking", true);
anim.SetBool("IsWalking", false);
}
}
else
{
anim.SetBool("IsIdle", true);
anim.SetBool("IsWalking", false);
anim.SetBool("IsStriking", false);
}
}
}