playMaker

Author Topic: Raycast passing through object  (Read 1573 times)

cardo

  • Playmaker Newbie
  • *
  • Posts: 19
Raycast passing through object
« on: April 25, 2020, 05:03:18 AM »
Hi all,

I have a really basic setup with a player character that will shoot a raycast from the camera when mouse1 is pressed. When the raycast hits an enemy capsule it sends an event to the enemy to subtract 1 from its health.

The issue is that the raycast seems to only hit the enemy capsule a small percentage of the time and will happily pass through the object other times when looking at the raycast debug line.

Is this a known issue? I've searched various forums for the answer but assuming it's something simple I've missed in my setup below. Any help would be appreciated!






cardo

  • Playmaker Newbie
  • *
  • Posts: 19
Re: Raycast passing through object
« Reply #1 on: April 25, 2020, 05:59:27 AM »
So at this point the enemy has moved about and been damaged a couple of times, and now in this position I'm not able to hit the enemy at all no matter how much I fire at it.

I've highlighted the enemy's AI state where he's currently looking for the player in case this has something to do with the player raycast not being able to hit it? It doesn't seem as though the enemy raycast is hitting the player capsule either for some reason otherwise it would be moving towards the player.




djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7616
    • jinxtergames
Re: Raycast passing through object
« Reply #2 on: April 25, 2020, 10:40:13 AM »
Hi.
you might need to use Layer mask so it only hits enemies correct layer
if you have child objects with colliders it might be hitting that.
OR you are hitting the player or gun.

also repeat interval is set to none. Set 0 to raycast once only.

also turn on 'Debug' (check box bottom right) so you can se the results.

to debug you can also use breakpoints (right click on a state to select/deselect breakpoints)
this will pause the game when reaching that state. that state is not yet executed you can set a breakpoint to the next state.

also for testing you might want to enable debug on the raycast AND Set repeat interval to 1 (1ce every frame) so you can see the raycast direction and range

cardo

  • Playmaker Newbie
  • *
  • Posts: 19
Re: Raycast passing through object
« Reply #3 on: April 26, 2020, 04:54:26 AM »
Thanks djaydino, I’ll try changing the repeat interval.

Currently when I fire I can already see the yellow debug ray going through the enemy capsule, so I assumed that it’s not getting caught on the player and is correctly going towards the enemy.

I haven’t tried any breakpoints because it does correctly cycle through each state, but it has a tendency to get back into the idle state and is unshootable.

I can’t see why the idle state would make the enemy invulnerable and seem to think that raycasts are only hitting the enemy when he’s at a certain rotation towards the player perhaps?

The enemy would become vulnerable again when he sees me and starts moving again, but again I’m not sure if it’s the state he’s in or his rotation.

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7616
    • jinxtergames
Re: Raycast passing through object
« Reply #4 on: April 26, 2020, 09:19:37 AM »
Hi.
Have you set layers?
because if the start point from the raycast is inside (for example) the player object then it will hit that, as it's the first thing it detects.

the debug line will only show the length of you raycast not from what it hits.
so if you see it goes tru the enemy it still does not mean that it is hitting it.

cardo

  • Playmaker Newbie
  • *
  • Posts: 19
Re: Raycast passing through object
« Reply #5 on: April 26, 2020, 10:54:43 AM »
I’m just trying to find some tutorials on layer masks and actually found some of your posts on here from 2017 djaydino  :D

So would I set up an enemy layer and geometry layer? Something like that? I don’t want the ray war to completely ignore the scenery etc. I’m guessing that if I just had it detecting enemy on a layer I’d be able to shoot through walls?

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7616
    • jinxtergames
Re: Raycast passing through object
« Reply #6 on: April 26, 2020, 01:41:26 PM »
hi.
yes you should detect for enemy and walls and other object that you wish to hit.
but for example not Player or Default