How Do I Destroy A Specific Object Instance With a Raycast?

0 favourites
  • 9 posts
From the Asset Store
112 High-Quality destruction sounds for videogames
  • I'm trying to set up a condition whereby when an object is intersected by a ray (and the player pushes a button), that specific instance of the object is destroyed. I have been able to set this up so that all instances of the object get destroyed, but not on a per-object basis. Is the best way to do this with UIDs, and if so, what would the event structure look like? My ray is otherwise working as intended and has a pretty straightforward setup, though I can post events if this helps.

  • Simply put, the cast ray doesn't pick any objects.

    You can use the


    If Ray intersected is true, the UID of the instance that was the first obstacle the ray intersected.

    expression in the 'Ray Intersected' event to identify the object hit. In a subevent, use the Sprite - Pick by unique ID condition to pick Object.Lineofsight.HitUID, and destroy object.

  • Brilliant, thank you very much!

  • Just an additional question; any idea why occasionally my objects aren't destroyed based on my current set of events? Everything looks like its set up properly, but I can't work out what is periodically causing the blocks not to get destroyed when it looks like the conditions are met. I've uploaded the whole project if anyone can help.

    This is what I'm currently seeing; I've also tried removing the ammo variable check. The right instance seems to be selected (i'm outputting it to a debug box in the top left corner).

  • Removing the ammunition check and disabling your recoil events makes it work perfectly. In some cases the ammunition variable is being changed prior to the trigger event that handles the destruction of the object. The recoil is affecting the aim as it is not working the way you intended. If you watch the debugger you will see it constantly changing values.

    It would be better to restructure your events so that all events that happen on the trigger (right shoulder pressed) are sub events of that condition. This way you will have more oversight and control of the order of execution.

    Currently you have two separate (right shoulder pressed) events which can cause the issue you are experiencing because the actions in each separate event are related.

  • Ahh, thank you for the insight - the event order makes a lot of sense, I'll restructure the events so that the execution order is logical.

    I've also just noticed the continually changing values for both of the Recoil values. Are you able to suggest changes to the events that would alleviate this? They're both supposed to return to 0. The recoil events are probably the ones I've struggled with most!

    Any help is massively appreciated.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • I'd focus on the other aspects first and then when they are working correctly, rethink the recoil.

    You want the recoil events to happen after the aim/destroy events so that it does not interfere with the user's aim. I would suggest using Tweens to ensure your values return to what you want them too. Unless you use lerp in a specific way the values will not return to zero. The main thing though is that you don't want those recoil events happening on every tick.

    You could have a boolean instance variable called "recoil" and set it to true after the aim/destroy actions. Then use that state to trigger the tweens for your recoil. When the tween that returns the value to the original number then you can set the recoil boolean to false.

  • Okay, that's really helpful - thank you. Before I go down the path of replacing the Lerp recoil events with Tweens, I just wanted to ask whether you thought they would allow me to implement the recoil more or less as it functions now (ignoring the cycling values from the Lerp of course!)

    Something that I have working at the moment is that if the player shoots in rapid succession, the recoil throws off the aim (which is intentional). If I get a Tween working, is it possible to 'interrupt' the animation so that it works in this way?

  • Tween is good for precise start and end values. It should work fine for your purpose.

    You can cancel the previous tween and start a new one for consecutive shots. You can also change the tween ease so that it's not linear.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)