Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
2
Question by Jason Hodges · Jun 23, 2011 at 09:37 AM · offsetturret

Turret shooting sometimes changes offset (why?)

Hello!

Short story; I have coded an offset into some turrets but they don't stick to the offset - they sometimes randomly change and I've no idea why!

Long story;

I have the following code which shoots a bullet every 2 seconds. And then shoots the next bullet in the array.

When they hit certain colliders they reset to the initial position and velocity (stationery).

(In case you're wondering why I haven't simply instantiated and destroyed them it's because that takes a lot of processing power on iOS).

 var sodBullets : Rigidbody[];
 var bulletForce : float;
 var shootInterval : float = 2;
 var offset : float;
 var lastShot : float = 0;
 var bulletIndex = 0;

 function Update()
 {
     if (Time.timeSinceLevelLoad + offset > lastShot + shootInterval) 
     {
         sodBullets[bulletIndex].rigidbody.velocity = transform.TransformDirection(Vector3(0,0,bulletForce));
         lastShot = Time.timeSinceLevelLoad + offset;
         bulletIndex++;
         if (bulletIndex > (sodBullets.length-1))
         {
             bulletIndex = 0;
         }
     }
 }


I have several of these turrets shooting with a slightly different offset to one another. For example the first is at 0 seconds offset, the second is at 0.5, the third at 1.0 and the fourth at 1.5 therefore they should hopefully all shoot one after another every half a second. But a bullet will only come out of the same turret every two seconds. Hopefully that made sense!

The problem is that this works for a few times at first and then after playing for a little while (not sure how long as it's always different) they start shooting at different offsets which appears to have been changed randomly.

Any ideas?

Same post but on Unity Forums; http://forum.unity3d.com/threads/94122-Hard-coded-shooting-offset-sometimes-randomly-changes-(why-)?p=611510#post611510

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

1 Reply

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

Answer by aldonaletto · Jun 23, 2011 at 11:23 AM

You should modify the timing control to avoid cumulative errors. I've changed your script a little: instead of saving the lastShot time, it calculates the nextShot time by adding the shootInterval to it.

 var sodBullets : Rigidbody[];
 var bulletForce : float;
 var shootInterval : float = 2;
 var offset : float;
 var nextShot : float = 0;
 var bulletIndex = 0;
 
 function Update()
 {
     if (Time.time >= nextShot + offset) 
     {
         sodBullets[bulletIndex].rigidbody.velocity = transform.TransformDirection(Vector3(0,0,bulletForce));
         nextShot += shootInterval;
         bulletIndex++;
         if (bulletIndex >= sodBullets.length)
         {
             bulletIndex = 0;
         }
     }
 }





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 Jason Hodges · Jun 23, 2011 at 11:52 AM 0
Share

Awesome, thanks dude. Yea I noticed that the time was being calculated slightly incorrectly although I assumed that each turret would have the same cumulative errors and therefore no significant effect although this was not the case. This has sorted it out perfectly, thanks again.

avatar image aldonaletto · Jun 23, 2011 at 03:35 PM 0
Share

There's a $$anonymous$$or issue in my script: since I used Time.time, the nextShot variable should be initialized in the Start function:

 function Start(){
 
   nextShot = Time.time;
 }

If a turret was instantiated some time after the level was loaded (or in a new level) it would fire repeatedly like a crazy Rambo until nextShot reached the current Time.time - this would not happen with Time.timeSinceLevelLoad, as in your original script.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

how to disable patent components incode 0 Answers

Turret AI script off rotation 1 Answer

Look to the side-Scripting Problem 2 Answers

Simple LookAt Rotation with offset pivot 2 Answers

Uneven mesh yet heights are all equal to one. 0 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