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 reberk · Mar 19, 2014 at 03:52 PM · 2djavascriptdistancetimestep

Syncing up two GameObjects' timestep

I have two GameObjects that, according to my code, move towards and then attack each other once within a certain range. The effect of the attack is a small upwards/backwards knockback, which, once the target has returned to the ground, results in the two GameObjects moving towards each other again to repeat the cycle.

The issue I'm having is that, even when both of these units are given the same parameters concerning attack range, only one of them will successfully attack, launching the other before it has a chance to respond. It seems to be random which of the two will "strike first," despite their running off of identical scripts. Ideally I'd like them to both successfully attack upon meeting, knocking each other back.

Right now, their attack trigger is a simple Vector2.Distance calculation: "if it's less than the attack range (and attack is not on cooldown), then attack." I had this in FixedUpdate and recently moved it to Update, but the behaviour is the same in either.

Is there a way to ensure that these separate GameObjects do their critical distance calculation on the same timestep? Or is there another way to go about this? Thank you for your counsel.

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
0
Best Answer

Answer by StephanK · Mar 19, 2014 at 04:28 PM

Executing of scripts is sequential, so if the first one knocks back his enemy the other one will be out of range when it's his turn (since he got knocked back). The safest way to do this is probably to have the attacker trigger the defendants attack method when he is attacking.

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 reberk · Mar 19, 2014 at 04:47 PM 0
Share

Ah! That's helpful to know. Because I want to have units with varying attack ranges, what I'll probably do is have any attack trigger a distance check on the target which, if it passes (and that unit isn't on cooldown), will result in a response attack (with a flag so as not to trigger an infinite string of such exchanges).

Just to clarify my understanding further: with the above plan in $$anonymous$$d (and the assumption that being the victim of an attack precludes the regular attack option), would two attack methods being called simultaneously be impossible? Or just very unlikely?

Thanks for your response.

*Edit: I just implemented this and it works beautifully. Thanks again (but still curious regarding my above synchrony question).

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

21 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Javascript Distance Check Not Working 1 Answer

How would I make a route that shows where player moves beforehand? 0 Answers

Changing material orientation dependant on mouse location? 0 Answers

2D Smoke or Particle 2 Answers

Collision with renderer.enabled? 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