I have been working on an AI using Playmaker and A* and have had decent success. The NPC can check if it can hear or see the player and then move to the player, avoiding obstacles. It works great but I want it to move to just shy of the player. I have it set up so it always moves to the same location by the player. That was easy enough - just use vector add and add 1 to the X and/or Z and it works fine but I want it to move to a spot near the player depending on the direction they are coming from towards the player. So if it is coming from down, it stops 1 down from the player. If it’s coming from the left, it stops to the left of the player. Suggestions?
Pseudo C# code:
public Transform playerTransform;
public float moveSpeed = 1.0f;
public float rotationSpeed = 1.0f;
public CharacterController enemyController;
private float distanceToStop = 1.0f;
private float distanceToPlayer = 0.0f;
private Vector3 targetDir = Vector3.zero;
//private Quaternion targetRotation = Quaternion.Identity;
void Update()
{
//smooth rotation
transform.rotation = Quaternion.Slerp(transform.rotation, playerTransform.rotation, Time.deltatime * rotationSpeed);
distanceToPlayer = Vector3.Distance(playerTransform.position, transform.position);
if(distanceToPlayer > distanceToStop)
{
targetDir = (playerTransform.position - transform.position);
targetDir.Normalize();
//enemyController.simpleMove(targetDir * moveSpeed * Time.deltatime);//simpleMove takes into account gravity
enemyController.Move(targetDir * moveSpeed * Time.deltatime);
}
else
{
//player is close enough, kill him! ^^
}
}
Note: I’ve blindly written this code here without any syntax check, so fix them out if you find any. ^^
Cheers,
Thanks! I’m going to be “converting” this to playmaker so your errors won’t be a problem. I’ll have a whole different set of errors.