- Home /
My object still follows when dead
How can I stop its position before it dies ? I'm using navmesh agent.
function Start(){
animation.GetClip("walk01").wrapMode = WrapMode.Loop;
animation.GetClip("death").wrapMode = WrapMode.Once;
animation.Play("walk01");
health = 3;
player = GameObject.Find("Player");
zombie = GameObject.Find("zombie");
attacking = false;
}
function OnTriggerEnter(other : Collider){
if(other.tag == "shot"){
health--;
Destroy(other);
}
}
function Update(){
playerDistance = Vector3.Distance(player.transform.position, transform.position);
if(playerDistance <= 1){
if(!attacking){
Invoke("ApplyDamage", 1);
attacking = true;
}
}
if(health <= 0){
die();
}
}
function ApplyDamage(){
player.SendMessage("SubstractHealth");
attacking = false;
}
function die(){
if(!isdead){
animation.Play("death");
Destroy(gameObject, 3);
isdead = true;
}
}
and my other scripts for following.
var target : Transform; function Update(){ GetComponent(NavMeshAgent).destination = target.position; }
Answer by Deadcow_ · Oct 28, 2014 at 09:22 AM
Cache NavMeshAgent like this
var n = player.GetComponent<NavMeshAgent>();
on Die function you can put
n.Stop();
or at top of Update:
if (isDead)
{
n.velocity = 0;
return;
}
Where should I put that var n and what is the data type of n ?
you may just put this at the end of the Die function:
player.GetComponent<Nav$$anonymous$$eshAgent>().Stop();
it should help
var is a shortening for any type you put in it on a declaration.
in my example it'll mean same thing:
Nav$$anonymous$$eshAgent n = player.GetComponent<Nav$$anonymous$$eshAgent>();
But this is my mistake, because my example assume, that you'll use this n variable in different places, so you need to declare it in global scope. To make it even easier you can declare public variable
public Nav$$anonymous$$eshAgent PlayerAgent;
and put Nav$$anonymous$$eshAgent to this script from Unity inspector
By the way, sorry for my terrible english, hope it's readable :)
Thank you, I have made it. your scripts are a little confusing. I use this in the end of my die function.
this.GetComponent(Nav$$anonymous$$eshAgent).Stop();
Oh, I see. You had used GameObject.Find(); so I thought current script and player script is placed on different objects. GameObject.Find() is really slow function and it's better to avoid to use it. In your case you can just use
player = GetComponent<Player>();
it'll work way faster :)
Answer by SnotE101 · Oct 28, 2014 at 02:20 AM
//when calling method die do this
//right now the method die is being called every frame that players health is less than 0 This will continue to update the player position. You need to make the method be called only once. I suggest using a boolean that is true at the start of the game and turned off when the function is called once.
die(this.transform);
//change method die to this.
function die(Transform pPosition)
{
if(!isdead){
animation.Play("death");
Destroy(gameObject, 3);
isdead = true;
this.transform.position = pPosition.position;
this.transform.rotation = pPosition.rotation;
}
}
I still don't understand what all you said, but I have fix it, beco$$anonymous$$g like this. function Start(){ animation.GetClip("walk01").wrap$$anonymous$$ode = Wrap$$anonymous$$ode.Loop; animation.GetClip("death").wrap$$anonymous$$ode = Wrap$$anonymous$$ode.Once; animation.Play("walk01"); health = 3; target = GameObject.FindWithTag("Player").transform; player = GameObject.Find("Player"); zombie = GameObject.Find("zombie"); attacking = false; move = true; }
function OnTriggerEnter(other : Collider){
if(other.tag == "shot"){
health--;
Destroy(other);
}
}
function Update(){
if(move){
GetComponent(Nav$$anonymous$$eshAgent).destination = target.position;
}
playerDistance = Vector3.Distance(player.transform.position, transform.position);
if(playerDistance <= 1){
if(!attacking){
animation.Play("attack01");
Invoke("ApplyDamage", 1);
attacking = true;
}
}
if(playerDistance > 1 && !attacking && health > 0){
animation.Play("walk01");
}
if(health <= 0){
die();
}
}
function ApplyDamage(){
player.Send$$anonymous$$essage("SubstractHealth");
attacking = false;
}
function die(){
if(!isdead){
animation.Play("death");
Destroy(gameObject, 3);
isdead = true;
move = false;
}
}
It doesn't follow me but it still move forward when dead.
Your answer
Follow this Question
Related Questions
Camera rotation around player while following. 6 Answers
Lock player position 1 Answer
Mecanim: Stop/Hold Animation at the end 3 Answers
position coins in parabolic path 1 Answer
Making a cube crouch 1 Answer