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 Athalansy · Oct 09, 2014 at 05:18 AM · raycastexplosionbomb

Raycast doesn't update

Hello! Let me explain the scenario first.. I have a character which places a bomb at the same spot he is currently. After 2.6sec bomb should explode and destroy what's in its way, that should be the box when character is out of way.

The problem is that it always kills the character too, although I set 2.5 sec delay, and if the last updated hit returns box, shouldn't it destroy only a box?

I tried using coroutines to time Raycast and Destroy just before bomb disappears, but nothing happens when bomb disappears...

Here's the destroy part of the code:

  void FixedUpdate () {         
     RaycastHit2D hit = Physics2D.Raycast (bomb.transform.position, Vector2.up, 2);
     Debug.Log (hit);
     Destroy (hit.collider.gameObject,2.5f);
 }



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

Answer by Baste · Oct 09, 2014 at 01:45 PM

The raycast happens instantly, and records the first object it hits. When you set 2.5f delay on the Destroy, that delays the Destroy, not what gets destroyed.

What happens in your code now is that every fixed update, which is quite often - the bomb checks what's directly above it, and sets that object to be destroyed in 2.5 seconds.

Assuming you place the bomb by using Instantiate, you get the behavior you want by using a Coroutine, like this:

 void Start() {
     StartCoroutine(DoDestroy());
 }
 
 IEnumerator DoDestroy() {
     yield return new WaitForSeconds(2.5f);
     RaycastHit2D hit = Physics2D.Raycast (bomb.transform.position, Vector2.up, 2);
     Debug.Log (hit);
     Destroy (hit.collider.gameObject,2.5f);
 
 }

Note that the bomb still only destroys stuff that's directly above it. If you want to destroy everything around it, you need to use Physics2D.OverlapCircleAll.

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 Athalansy · Oct 09, 2014 at 05:10 PM 0
Share

Hello @Baste! Thanks for your help.. I've tried what you recommended, it seems that the raycast recognizes does execute after 2.5sec delay, but now It won't destroy the box..

Here's the code, I've been trying to figure this out myself for 16 hours now but I just can't get it to work.. I've added the coroutine to PlaceBomb script because when it was in it own script I don't know how to target Bomba(Clone) in other script..

http://www.codeshare.io/rQGFN

Also thanks for the Physics2d.OverlapCircleAll, I wanted to do achieve similar effect with 4 raycasts in the first place but I'll do it with that class now :)

Thanks again!

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

NEED HELP! My animator Keeps returning to "Any State", HELP! 0 Answers

OverlapSphere not causing damage? - Solved 2 Answers

Raycast or RaycastAll? And how? 1 Answer

Raycast not detecting distance of my GameObject 1 Answer

How to Spherecast/Raycast diagonally? 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