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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by DMCH · Apr 17, 2013 at 04:15 PM · c#rotationsoundturret

Play Sound when turret rotates

Hello,

Thanks for reading. Having some difficulty getting a sound to play when a turret rotates. At present, the sound plays continually when an enemy is near, i.e. even if the turret's barrel is perfectly aligned with the target.

Here's the code for turret rotation:

         // Plays the sound of turret rotating
         PlayRotateTurretSound();
         
         // Get the distance to the enemy
         float distanceToEnemy = Vector3.Distance(targetPosition, transform.position);
         Debug.Log ("TurretGunScript.RotateTurret: Distance to enemy : " + distanceToEnemy);
         
         // Get the amount of time it will take for bullet to hit enemy
         float timeToHit = distanceToEnemy/bulletSpeed;
         Debug.Log ("TurretGunScript.RotateTurret: Bullet travel time : " + timeToHit);
         
         // Get the velocity of the target rigidbody and multiply by amount of time to hit
         Vector3 targetVelocity = (target.rigidbody.velocity * timeToHit) * 1.5f; // This compensates for some descrepancy, presumably in bulletSpeed
         
         // Add the velocity * time to the target's curret position
         Vector3 finalPos = targetPosition + targetVelocity;
         
         // Get the direction to aim the turret
         Vector3 aimDirection = finalPos - bulletSpawn0.transform.position;
         
         // Rotate turret to face that position
         Quaternion neededRotation = Quaternion.LookRotation(aimDirection);
         transform.rotation = Quaternion.Slerp(transform.rotation, neededRotation, rotationSpeed * Time.deltaTime);

I'm guessing this could be achieved by using an if statement around the instruction to play the sound, and requiring a minimum change in direction to the target, but I'm a little weak on angles and quaternions, and don't know how to do this. Any help greatly appreciated.

Comment
Add comment · Show 1
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 DMCH · Apr 17, 2013 at 05:52 PM 0
Share

Following kmeboe's solution, this is the new code.

 // Store current rotation
         Quaternion oldRotation = transform.rotation;
         
         // Get the distance to the enemy
         float distanceToEnemy = Vector3.Distance(targetPosition, transform.position);
         Debug.Log ("TurretGunScript.RotateTurret: Distance to enemy : " + distanceToEnemy);
         
         // Get the amount of time it will take for bullet to hit enemy
         float timeToHit = distanceToEnemy/bulletSpeed;
         Debug.Log ("TurretGunScript.RotateTurret: Bullet travel time : " + timeToHit);
         
         // Get the velocity of the target rigidbody and multiply by amount of time to hit
         Vector3 targetVelocity = (target.rigidbody.velocity * timeToHit) * 1.5f; // This compensates for some descrepancy, presumably in bulletSpeed
         
         // Add the velocity * time to the target's curret position
         Vector3 finalPos = targetPosition + targetVelocity;
         
         // Get the direction to aim the turret
         Vector3 aimDirection = finalPos - bulletSpawn0.transform.position;
         
         // Rotate turret to face that position
         Quaternion neededRotation = Quaternion.LookRotation(aimDirection);
         transform.rotation = Quaternion.Slerp(transform.rotation, neededRotation, rotationSpeed * Time.deltaTime);
         
         // If old rotation is different from current rotation, play rotation sound
         if(oldRotation != transform.rotation)
         {
             PlayRotateTurretSound();
         }

1 Reply

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

Answer by kmeboe · Apr 17, 2013 at 05:42 PM

One good solution to this would be to store the transform.rotation before you apply the Slerp, then compare the new transform.rotation with the stored one (using ==) and see if they are different; if so, play the sound.

See if you can work up the new script for this. If you get stuck, post your new script, and I'll help you a bit more.

Edit: another solution, even better, would be to compare "neededRotation" with Quaternion.identity before you call the slerp. If they're NOT equal, then do the rotation and play the sound. I'm not that familiar with Quaternions, but this should work. Try this solution, and if it doesn't work then try the one above.

Comment
Add comment · Show 2 · 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 DMCH · Apr 17, 2013 at 05:51 PM 0
Share

Nice solution, thanks!

avatar image kmeboe · Apr 17, 2013 at 06:02 PM 0
Share

You're welcome.

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

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Flip over an object (smooth transition) 3 Answers

Turret Continuing to Max Rotation - Following Mouse Cursor 0 Answers

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

Need to limit the rotation angles on a turret 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