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 networkZombie · Mar 24, 2011 at 11:58 PM · javascripterrorgame

Scripting error #2!

This is my AI script for the enemies in my game. My problem is that it plays the shoot sound but for some reason it doesn't play the dieSound. Here's my script -

var target; //target = transform.Find("Character"); var damp : int = 3; var attackRange = 3; // safe distance from the player var followRange = 50; //distance required for enemy to start following var bulletPrefab : Transform; var savedTime = 0; var explosionPrefab : Transform; var killsNeeded = 10; var currentLevel = 2; var shootSound : AudioClip; var dieSound : AudioClip;

var hitPoints = 3;

function Update () {
target = GameObject.FindWithTag("Player");

if((followRange > Vector3.Distance(target.transform.position, transform.position )) && (attackRange != Vector3.Distance(target.transform.position, transform.position ))) // if the enemy is closer than the follow range { follow(target); }

var seconds : int = Time.time; var oddeven = (seconds % 2);

if ((attackRange >= Vector3.Distance(target.transform.position, transform.position )) &&(oddeven)) Shoot(seconds); }

function follow(target) { if( attackRange < Vector3.Distance(target.transform.position, transform.position)) //if enemy is farther than attack range { transform.LookAt(target.transform); //look at target transform.Translate (0,0,6*Time.deltaTime, Space.Self ); //move towards target } }

function Shoot(seconds) { if(seconds != savedTime) { audio.PlayOneShot(shootSound); var shoot = Instantiate(bulletPrefab, transform.Find("BulletSpawn").transform.position, Quaternion.identity); shoot.gameObject.tag = "enemyProjectile"; shoot.rigidbody.AddForce(transform.forward * 1000);

 savedTime = seconds;

} }

function OnTriggerEnter(hit : Collider) { if(hit.gameObject.tag == "fallout") { Die(); }

if(hit.gameObject.tag == "playerProjectile") { hitPoints -= 1; //Destroy(hit.gameObject);

 if(hitPoints &lt; 1)
 {
     Die();
 }

} }

function Die() { audio.PlayOneShot(dieSound); MoveAround.kills++; botMaker.AlienCount--; //MoveAround.scored = true;

if (MoveAround.kills >= killsNeeded) { Application.LoadLevel(currentLevel); healthControl = 6; MoveAround.kills = 0; botMaker.AlienCount = 0; currentLevel++; }

Destroy(gameObject); var explode = Instantiate(explosionPrefab, gameObject.transform.position, Quaternion.identity); }

Comment
Add comment · Show 2
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 AngryOldMan · Mar 25, 2011 at 12:55 AM 0
Share

where is the audio source for the die sound in the scene? it may be because you are to far away with logrithmic roll off on the audio source. Find the audio source, set its roll off to linear and make sure you're really close to the enemy when you kill it. If that doesn't work tell us more I'm sure we will do what we can to help :)

avatar image networkZombie · Mar 25, 2011 at 04:21 PM 0
Share

Its not the audio source because i hear the shoot sound when the enemies shoot.

2 Replies

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

Answer by Bunny83 · Mar 25, 2011 at 01:46 AM

You start playing a sound and in the same frame you destroy the object (including the audiosource). You can either create a temporary GO with an audiosource to play the die sound or you have to delay the Destroy(gameObject);

The Destroy function have a second parameter that allows you to specify a delay in seconds. If the die-sound length is 2 seconds you could do:

Destroy(gameObject,2.2);
Comment
Add comment · Show 3 · 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 networkZombie · Mar 25, 2011 at 04:39 PM 0
Share

Ill try that and tell you if it works.

avatar image networkZombie · Mar 26, 2011 at 12:41 AM 0
Share

Thank you that works. I dont know why I didnt think about that I feel stupid now :(

avatar image Bunny83 · Mar 26, 2011 at 02:50 AM 0
Share

Don't feel stupid. There are million ways of doing something. And a lot of things are not obvious if you haven't worked with it. Same goes for coroutines. If you delete a GameObject on which a coroutine is running, it will be ter$$anonymous$$ated. $$anonymous$$y way is just a way, not "the" way :D.

avatar image
0

Answer by DaveA · Mar 25, 2011 at 01:06 AM

Double check that it is assigned to this script, that it's volume settings are kosher, all that.

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 networkZombie · Mar 25, 2011 at 04:22 PM 0
Share

i checked everything

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

No one has followed this question yet.

Related Questions

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

Setting Scroll View Width GUILayout 1 Answer

Loading next level, but next level never gets incremented. 3 Answers

I am trying to make a movement script for my main camera but it isnt working! 2 Answers

How do i enter the UnityEngine.Transform instance? 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