- Home /
Question by
DanProd · Feb 23, 2019 at 04:36 AM ·
playerscript.ainavmeshagentscriptingproblem
Runaway AI
Hello good day guys! I have some i guess, well simple problem. I cant seem to find how i would make the enemy runway from the player when low in health? here is the code for the health: { public float enemyHealth = 25; public GameObject DeadPrefab;
public void TakeDamage(float amnt)
{
enemyHealth -= amnt;
if(enemyHealth <= 0)
{
Instantiate(DeadPrefab, transform.position, transform.rotation);
{
Destroy(this.gameObject);
}
}
//survived!
if (enemyHealth <= 20)
{
//runawayfunctionhere
}
}
}
And Here is for the AI:
UnityEngine.AI.NavMeshAgent agent;
public Transform target;
public float extraRotationSpeed;
void Start ()
{
target = GameObject.FindWithTag("Player").transform;
agent = GetComponent<UnityEngine.AI.NavMeshAgent> ();
}
void Update()
{
extraRotation();
agent.SetDestination(target.position);
}
void extraRotation()
{
Vector3 lookrotation = agent.steeringTarget - transform.position;
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(lookrotation), extraRotationSpeed * Time.deltaTime);
}
Comment
Answer by paulbuck86 · Feb 23, 2019 at 05:26 AM
Perhaps something like:
void Update()
{
if (enemyhealth <= 25)
{
extraRotation();
}
agent.SetDestination(target.position);
}
void extraRotation()
{
Vector3 lookrotation = agent.transform.position - player.transform.position;
transform.Translate (lookrotation.normalized * extraRotationSpeed * Time.deltaTime);
}
Your answer
Follow this Question
Related Questions
Enemy's NavMesh not working 2 Answers
NavMesh flee. Ai flee from player. 4 Answers
NavMeshAgent does not follow player 0 Answers
Tank not moving towards player 1 Answer
NavMeshAgent track Player when in Range? 2 Answers