playMaker

Author Topic: [SOLVED]Rotating Raycast for Enemy-AI  (Read 5520 times)

Mayhem

  • Full Member
  • ***
  • Posts: 171
[SOLVED]Rotating Raycast for Enemy-AI
« on: May 31, 2013, 11:44:41 AM »
Hey there. So I watched the great "Basic-AI-Tutorial Series" and customized it for my personal need on a 2D Plattforming Game.

What do I want to do?

The Main-System of the AI is pretty much the same like in the Tutorials of BadSeedGames, only the Movement-FSM and the Orientation-FSM are changed, but the Problem isn't the Movement-FSM it's the Orientation. Here's mine:



What am I doing? So, first of all I'm checking if the Enemy is headed to the Left or the Right, that I'm checking with the XScale of the Enemy-Position and depending on the value the Raycast is firing left or right.



This is actually working, but the problem is the System itself. The Raycast only starts firing if the Character is in Range, and then it is stuck in it's direction (Left or Right).

This is the "normal" behaviour:
1. Enemy is patrolling left to right , no Raycast
2. Player comes from the Left, Enemy is heading in Players direction
3. Player is in Range, Raycast is Firing to the left and hits the Player
4. Other States are triggered (Chasing etc.)

Fine.

And this is the "unnormal" behaviour:

1. Enemy is patrolling left to right , no Raycast
2. Player comes from the Left, Enemy is heading in Players direction
3. Player waits till Enemy comes to the point where he's turning himself around to patrol in the opposite direction.
4. Player follows Enemy, which is patrolling now to the right.
5. Player is in Range, Raycast is Firing to the right, although Player is on the Left side of the Enemy



6. Enemy is stuck in the "Raycast"-Action, because there is nothing to hit on the other side.



The Enemy stops only Raycasting when the Player is out of Range again, which is fine, but it's kind of dumb when the Enemy is facing the opponent but isn't chasing him...

Any ideas how one can solve this issue?

I thought about rotating the Ray but I don't know exactly how to do that.
« Last Edit: June 06, 2013, 10:57:09 AM by Mayhem »

Mayhem

  • Full Member
  • ***
  • Posts: 171
Re: Rotating Raycast for Enemy-AI
« Reply #1 on: June 05, 2013, 07:35:11 AM »
No ideas/suggestions for this?

Lane

  • Administrator
  • Hero Member
  • *****
  • Posts: 2511
  • Mender of the past
    • Cleverous
Re: Rotating Raycast for Enemy-AI
« Reply #2 on: June 05, 2013, 08:05:36 AM »
Hmm, weird system. Not sure how it is actually intended to work.

In the start state where you define which direction to fire the ray I think it's backwards, Greater is positive (and usually to the Right) and Lesser is negative (left). That may explain a ray pointing the wrong direction but I'm not sure how your game view is oriented.

Are you triggering the Start state again when he changes direction? The raycasting state seems to be an idle state waiting for a hit, but when it changes direction you have to flip it and go to the other state.

Kind of hard to tell whats going on, a webplayer with debug stuff on would help.
Products by Cleverous
|| Vault Core : Database
|| Vault Inventory : Multiplayer Inventory
|| Vault Attributes : Character Stats
|| That Hurt! : Dmg Floaties
|| Quinn : 3D

Mayhem

  • Full Member
  • ***
  • Posts: 171
Re: Rotating Raycast for Enemy-AI
« Reply #3 on: June 05, 2013, 08:17:56 AM »
Well I wrote how it actually intends to work:

Quote
This is the "normal" behaviour:
1. Enemy is patrolling left to right , no Raycast
2. Player comes from the Left, Enemy is heading in Players direction
3. Player is in Range, Raycast is Firing to the left and hits the Player
4. Other States are triggered (Chasing etc.)

The Game View is set to "Back".

And no, the Start-State is not triggered again when changing direction.

Actually I think I just need a way to rotate the Raycast. Is there an Action for that?

Lane

  • Administrator
  • Hero Member
  • *****
  • Posts: 2511
  • Mender of the past
    • Cleverous
Re: Rotating Raycast for Enemy-AI
« Reply #4 on: June 05, 2013, 08:31:32 AM »
Well I wrote how it actually intends to work:

Yeah, it just seems like a strange approach - to me.

Is the Sending Raycast Right firing x+1(self)?

Quote
And no, the Start-State is not triggered again when changing direction.

So how does it get into the other "Sending Raycast" state?

Quote
Actually I think I just need a way to rotate the Raycast. Is there an Action for that?

Not specifically, but you could expose it as a variable and change it from another fsm/state. That's pretty common.
Products by Cleverous
|| Vault Core : Database
|| Vault Inventory : Multiplayer Inventory
|| Vault Attributes : Character Stats
|| That Hurt! : Dmg Floaties
|| Quinn : 3D

Mayhem

  • Full Member
  • ***
  • Posts: 171
Re: Rotating Raycast for Enemy-AI
« Reply #5 on: June 05, 2013, 09:26:38 AM »
What would be a better aproach then? I just followed these AI-Series:
Quote
Is the Sending Raycast Right firing x+1(self)?

Raycast Right is firing in Direction: 1/0/0 - self, yes.

Quote
So how does it get into the other "Sending Raycast" state?

Well, I guess that's the "problem" :D

The Enemy is patrolling 5 Units to the left, stops, turns around and patrols 5 Units to the right again, and so on and on. The thing is, when the Enemy is patrolling the Raycast isn't fired, this system is "off", when the Player comes near it turns on and the raycast is firing in the direction it's facing.

So if the Enemy is walking right and the player's coming from the left the Enemy doesn't see him, but the Raycast starts firing to the right and of course it doesn't hit anything, so the Enemy is patrolling normally his 5 Units to the right, (and the Player is following him) then he stops, turns around and walks to the left. Since the Player is near enough the Raycast still is on, but though the Enemy turned around to the left and is patroling left the Raycast is still firing to the right.

ONLY when the Player goes back to the point the Raycast is off again and is nearing himself to the enemy facing him (player is looking the Enemy in his eyes), the Raycast is fired in the "correct" direction.

Quote
Not specifically, but you could expose it as a variable and change it from another fsm/state. That's pretty common.

Sorry, but I don't quite understand this, how should I do this.


And thanks for the help, really appreciate it :)

Lane

  • Administrator
  • Hero Member
  • *****
  • Posts: 2511
  • Mender of the past
    • Cleverous
Re: Rotating Raycast for Enemy-AI
« Reply #6 on: June 05, 2013, 09:37:35 AM »
If you want to use a variable to control the raycast direction then create a vector3 variable and assign it as the target vector in the raycast action instead of inputting the -1 or 1. Then you can change that variable from another FSM by using Set FSM Vector 3 and putting in your direction there.

This is assuming you don't have a better way of just running the scale test over again by just firing your "Enable" event so it starts your Raycasting FSM from the start and figures the direction out on its own.
Products by Cleverous
|| Vault Core : Database
|| Vault Inventory : Multiplayer Inventory
|| Vault Attributes : Character Stats
|| That Hurt! : Dmg Floaties
|| Quinn : 3D

Mayhem

  • Full Member
  • ***
  • Posts: 171
Re: Rotating Raycast for Enemy-AI
« Reply #7 on: June 06, 2013, 10:56:37 AM »
Well, I could manage it with the Vector3-Approach (which is way cleaner/better) :)
AND in the Raycast-Action there is a Setting called "Repeat Interval", which was set the entire time to 0. Once I set to 1 it updated it every Frame and so...yeah. It's working now :)