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 ZdzichuRonczka · Oct 25, 2014 at 08:33 PM · javascriptenemynavmeshagent

Script issues [js]

Before I tell what's going on, let me I put a video with the source code.

Video: http://www.indiedb.com/games/johnny-onion/videos/new-score-system-mobs-respawn-with-bugissue

EnemyHealth

 var startingHealth : int = 100;            // The amount of health the enemy starts the game with.
 var currentHealth : int;                   // The current health the enemy has.
 var sinkSpeed : float = 2.5f;              // The speed at which the enemy sinks through the floor when dead.
 var scoreValue : int = 10;                 // The amount added to the player's score when the enemy dies.
 var deathClip : AudioClip;                 // The sound to play when the enemy dies.
 var enemy : GameObject;
 var playerHealth : PlayerHealth;

 private var anim : Animator;                              // Reference to the animator.
 private var enemyAudio : AudioSource;                     // Reference to the audio source.
 private var hitParticles : ParticleSystem;                // Reference to the particle system that plays when the enemy is damaged.
 private var capsuleCollider : CapsuleCollider;            // Reference to the capsule collider.
 private var isDead : boolean;                                // Whether the enemy is dead.
 private var isSinking : boolean;                             // Whether the enemy has started sinking through the floor.

 function Awake ()
 {
     // Setting up the references.
     anim = GetComponent (Animator);
     enemyAudio = GetComponent (AudioSource);
     hitParticles = GetComponentInChildren (ParticleSystem);
     capsuleCollider = GetComponent (CapsuleCollider);

     // Setting the current health when the enemy first spawns.
     currentHealth = startingHealth;
 }

 function Update ()
 {
     // If the enemy should be sinking...
     if(isSinking)
     {
         // ... move the enemy down by the sinkSpeed per second.
         transform.Translate (-Vector3.up * sinkSpeed * Time.deltaTime);
     }
 }


 public function TakeDamage (amount : int, hitPoint : Vector3)
 {
     // If the enemy is dead...
     if(isDead)
         // ... no need to take damage so exit the function.
         return;

     // Play the hurt sound effect.
     enemyAudio.Play ();

     // Reduce the current health by the amount of damage sustained.
     currentHealth -= amount;
    
     // Set the position of the particle system to where the hit was sustained.
     hitParticles.transform.position = hitPoint;

     // And play the particles.
     hitParticles.Play();

     // If the current health is less than or equal to zero...
     if(currentHealth <= 0)
     {
         // ... the enemy is dead.
         Death ();
     }
 }

 function Death ()
 {
     // The enemy is dead.
     isDead = true;

     // Turn the collider into a trigger so shots can pass through it.
     capsuleCollider.isTrigger = true;

     // Tell the animator that the enemy is dead.
     anim.SetTrigger ("Dead");

     // Change the audio clip of the audio source to the death clip and play it (this will stop the hurt clip playing).
     enemyAudio.clip = deathClip;
     enemyAudio.PlayOneShot(deathClip);
 }

 public function StartSinking ()
 {
     // Find and disable the Nav Mesh Agent.
     GetComponent (NavMeshAgent).enabled = false;

     // Find the rigidbody component and make it kinematic (since we use Translate to sink the enemy).
     GetComponent (Rigidbody).isKinematic = true;

     // The enemy should no sink.
     isSinking = true;

     // Increase the score by the enemy's score value.
     ScoreManager.score += scoreValue;
     Spawn ();
    
     // After 2 seconds destory the enemy.
     Destroy (gameObject, 2f);
 }

 function Spawn (){
    // If the player has no health left...
     if(playerHealth.currentHealth <= 0f)
     {
         // ... exit the function.
         return;
     }
    Instantiate (enemy, transform.position, transform.rotation);
    Instantiate (enemy, transform.position, transform.rotation);
 }

EnemyMovement

 private var player : Transform;               // Reference to the player's position.
 private var playerHealth : PlayerHealth;      // Reference to the player's health.
 private var enemyHealth : EnemyHealth;        // Reference to this enemy's health.
 private var nav : NavMeshAgent;               // Reference to the nav mesh agent.

 function Awake ()
 {
     // Set up the references.
     player = GameObject.FindGameObjectWithTag ("Player").transform;
     playerHealth = player.GetComponent (PlayerHealth);
     enemyHealth = GetComponent (EnemyHealth);
     nav = GetComponent (NavMeshAgent);
 }


 function Update ()
 {
     // If the enemy and the player have health left...
     if(enemyHealth.currentHealth > 0 && playerHealth.currentHealth > 0)
     {
         // ... set the destination of the nav mesh agent to the player.
         nav.SetDestination (player.position);

     }
     // Otherwise...
     else
     {
         // ... disable the nav mesh agent.
         nav.enabled = false;
     }
 }

So yes, I'm learning a little bit of tutorials on unity - those about here http://unity3d.com/learn/tutorials/projects/survival-shooter - and something is going wrong, that i can't fix. In this film you can see it clearly, points are awarded properly but the problem is with the nav agent. The aim of such an arrangement script [in the original spawns are done in a separate component] is the simple fact that after the mob death, in circle of his place pops up two more on the animation 'birth' [in the simplest short, these beets pop out of the ground :)]. Well, I do not know how to fix these errors and I'm not a programmer - if anyone can help? [translated with little help from g.translate :) ]

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

0 Replies

· Add your reply
  • Sort: 

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

2 People are following this question.

avatar image avatar image

Related Questions

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

Simple Enemy AI not working : NullReferenceException: Object reference not set to an instance of an object 2 Answers

Collisions not working when player stands still 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers


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