Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Robby-rEINEWALD · Oct 27, 2014 at 04:25 PM · positionstopdead

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; }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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;
 }

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Robby-rEINEWALD · Oct 28, 2014 at 10:10 AM 0
Share

Where should I put that var n and what is the data type of n ?

avatar image Deadcow_ · Oct 28, 2014 at 10:28 AM 0
Share

you may just put this at the end of the Die function:

 player.GetComponent<Nav$$anonymous$$eshAgent>().Stop();

it should help

avatar image Deadcow_ · Oct 28, 2014 at 10:39 AM 0
Share

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 :)

avatar image Robby-rEINEWALD · Oct 29, 2014 at 01:07 AM 0
Share

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();

avatar image Deadcow_ · Oct 29, 2014 at 06:19 AM 0
Share

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 :)

avatar image
0

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;
      }
 }

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Robby-rEINEWALD · Oct 28, 2014 at 08:16 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges