playMaker

Author Topic: RayCast From GameObject  (Read 30196 times)

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
RayCast From GameObject
« on: December 07, 2011, 04:59:21 AM »
Hi,

ok, following several thread, I am working on simplifying the raycasting system in playmaker.

 as a start, here is a new raycasting action, that is simpler to set up taking a gameObject and a plain english direction to fir the ray.

[EDIT] this action is now available on the Ecosystem :)

Code: [Select]
// (c) Copyright HutongGames, LLC 2010-2011. All rights reserved.

using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.Physics)]
[Tooltip("Casts a Ray from a GameObject against all Colliders in the scene. Use GetRaycastInfo to get more detailed info.")]
public class RaycastFromGameObject : FsmStateAction
{

public enum Direction
{
Forward,
Backward,
Up,
Down,
Left,
Right
}

[Tooltip("Start ray at game object position")]
public FsmOwnerDefault fromGameObject;

[Tooltip("A direction: Forward (z), Backward (-z), up (y), Down (-y), Left (-x), Right (x) ")]
public Direction direction;

[Tooltip("The length of the ray. Set to -1 for infinity.")]
public FsmFloat distance;

[Tooltip("Event to send if the ray hits an object.")]
[UIHint(UIHint.Variable)]
public FsmEvent hitEvent;

[Tooltip("Set a bool variable to true if hit something, otherwise false.")]
[UIHint(UIHint.Variable)]
public FsmBool storeDidHit;

[Tooltip("Store the game object hit in a variable.")]
[UIHint(UIHint.Variable)]
public FsmGameObject storeHitObject;

[Tooltip("Set how often to cast a ray. 0 = once, don't repeat; 1 = everyFrame; 2 = every other frame... \nSince raycasts can get expensive use the highest repeat interval you can get away with.")]
public FsmInt repeatInterval;

[Tooltip("Pick only from these layers.")]
[UIHint(UIHint.Layer)]
public FsmInt[] layerMask;

[Tooltip("Invert the mask, so you pick from all layers except those defined above.")]
public FsmBool invertMask;

public FsmBool debug;

private GameObject go;

int repeat;

public override void Reset()
{
fromGameObject = null;
direction = Direction.Forward;
distance = 100;
hitEvent = null;
storeDidHit = null;
storeHitObject = null;
repeatInterval = 5;
layerMask = new FsmInt[0];
invertMask = false;
debug = false;
}

public override void OnEnter()
{
DoRaycast();

if (repeatInterval.Value == 0)
Finish();
}

public override void OnUpdate()
{
repeat--;

if (repeat == 0)
DoRaycast();
}

void DoRaycast()
{
repeat = repeatInterval.Value;

if (distance.Value == 0)
return;

Vector3 originPos;

go = Fsm.GetOwnerDefaultTarget(fromGameObject);

originPos = go.transform.position;


float rayLength = Mathf.Infinity;
if (distance.Value > 0 )
rayLength = distance.Value;

RaycastHit hitInfo;

Vector3 dir = GetDirectionVector();

Physics.Raycast(originPos, dir, out hitInfo, rayLength, ActionHelpers.LayerArrayToLayerMask(layerMask, invertMask.Value)); //TODO LayerMask support

Fsm.RaycastHitInfo = hitInfo;

bool didHit = hitInfo.collider != null;

storeDidHit.Value = didHit;

if (didHit)
{
Fsm.Event(hitEvent);
storeHitObject.Value = hitInfo.collider.collider.gameObject;
}

if (debug.Value)
{
float debugRayLength = Mathf.Min(rayLength, 1000);
Debug.DrawLine(originPos, originPos + dir * debugRayLength, Fsm.DebugRaycastColor);
}
}


Vector3 GetDirectionVector()
{
Vector3 dir = go.transform.forward; // by default

switch(direction){
case Direction.Backward:
dir = - go.transform.forward;
break;
case Direction.Up:
dir = go.transform.up;
break;
case Direction.Down:
dir = -go.transform.up;
break;
case Direction.Left:
dir = - go.transform.right;
break;
case Direction.Right:
dir = go.transform.right;
break;
}

return dir;
}
}
}





This will evolve over time to try and achieve something easy for beginners. So comments and suggestion most welcome!

bye,

 Jean
« Last Edit: December 08, 2017, 01:49:56 AM by jeanfabre »

jayhfd

  • Junior Playmaker
  • **
  • Posts: 71
Re: RayCast From GameObject
« Reply #1 on: December 13, 2011, 06:43:40 PM »
I've been using RayCasts from Playmaker the last couple of days and I gotta say, the simple "forward" ray cast firing is awesome. I had so much trouble working out how to do this! Many vector3, world/local/transform direction stuff fries my poor art brain!

Thanks again,
Jay

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: RayCast From GameObject
« Reply #2 on: December 14, 2011, 07:18:47 AM »
Hi,

 Good to know. I am planning on making it even easier with one action to raycast and process with the hit information straight away to a gameObject with hit normal AND hit reflection ( where the ray would go if it bounced off after this hit, but that's a for a bit later when I'll have some more free time or if you shout loud enough :)

 Until then... I have seen that it's still problematic to set up a raycast when you are beginning, so I made a package showing a working example of raycast implementation ( one out of many of course).

[EDIT] package is now available on the PlayMaker Ecosystem



Hopefully, this package will lift much of the issues regards setting up. If you have other solutions, don't hesitate to submit and share them, this is by no means THE solution...

 Questions welcome as always.

 Bye,

 Jean
« Last Edit: December 08, 2017, 01:49:12 AM by jeanfabre »

Dev_Sebas

  • 1.2 Beta
  • Sr. Member
  • *
  • Posts: 398
    • Blog
Re: RayCast From GameObject
« Reply #3 on: December 14, 2011, 09:46:33 AM »
This problem
Store?
Thanks for any help

Dev_Sebas

  • 1.2 Beta
  • Sr. Member
  • *
  • Posts: 398
    • Blog
Re: RayCast From GameObject
« Reply #4 on: December 14, 2011, 09:52:13 AM »
I have tried with a Get Mouse Button Down and Sent Event To Fsm in another state but it don´t work.
Bye
Thanks for any help

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: RayCast From GameObject
« Reply #5 on: December 15, 2011, 12:24:04 AM »
Hi,

The store field is there for you to save the state of the mousebutton into a FsmVariable.

Create a Global var and use this to store the mouse button and in another fsm to check for that fsm bool. Just like I do in my example.

if you want to send an event to another fsm, where is your actual problem? Is it the way to check if the mouse button is down and fire the event or do you fire the event properly but can't respond to on another fsm?

We'll get there :)

Bye,

 Jean

Dev_Sebas

  • 1.2 Beta
  • Sr. Member
  • *
  • Posts: 398
    • Blog
Re: RayCast From GameObject
« Reply #6 on: December 15, 2011, 04:42:21 PM »
Yes
As I know it´s for send a event to "firing system", it isn´t?
How can I put something on it? Global var isn´t working
Please help me
Bye

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: RayCast From GameObject
« Reply #7 on: December 16, 2011, 12:39:03 AM »
Hi,

 I am not sure I am following you.

 Are you willing to send data along your event? if so , simply add a "Set Event Data" prior firing your event. then when you receive that event, use "Get Event Info" to retrieve the data sent along with the event.

 Is that what you are after?

 Bye,

 Jean

Dev_Sebas

  • 1.2 Beta
  • Sr. Member
  • *
  • Posts: 398
    • Blog
Re: RayCast From GameObject
« Reply #8 on: December 16, 2011, 12:33:15 PM »
Thanks a lot Jean ;D finally
PlayMaker should have more tuts
Bye

FritsLyn

  • Full Member
  • ***
  • Posts: 191
Re: RayCast From GameObject
« Reply #9 on: April 22, 2013, 04:11:50 PM »

 Until then... I have seen that it's still problematic to set up a raycast when you are beginning, so I made a package showing a working example of raycast implementation ( one out of many of course).


http://dl.dropbox.com/u/17356625/Unity/projects/misc/raycast.unitypackage

 It shoots repeatably or as you press the mouse ( a fsm var is used for switching between the two behavior for the curious one amongst you : ). The red GameObject represent the hit normal.

Erh.. there is an error in there -> Mouse down store result variable is missing ;)

Lane

  • Administrator
  • Hero Member
  • *****
  • Posts: 2511
  • Mender of the past
    • Cleverous
Re: RayCast From GameObject
« Reply #10 on: April 22, 2013, 04:31:26 PM »
This thread is ancient, I would think a few errors are expected in something that old.
Products by Cleverous
|| Vault Core : Database
|| Vault Inventory : Multiplayer Inventory
|| Vault Attributes : Character Stats
|| That Hurt! : Dmg Floaties
|| Quinn : 3D

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: RayCast From GameObject
« Reply #11 on: April 23, 2013, 02:42:54 PM »
Hi,

 Double checked, that package is still very much working. What version of playmaker are you using and what version of Unity?

bye,

 Jean

Andys

  • Playmaker Newbie
  • *
  • Posts: 30
Re: RayCast From GameObject
« Reply #12 on: June 16, 2013, 04:15:37 PM »
Hi Jean
Can you explain how this is supposed to work, as the events just stays on the Delay Fire state, as already stated above, the mouse click seems to do nothing as the store result is None, how am i supposed to send the mouse click event to the firing system?

I would have thought by clicking the mouse we would have seen the firing system run through, any help would be appreciated

Andy

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: RayCast From GameObject
« Reply #13 on: June 17, 2013, 06:25:13 AM »
Hi,

not sure what happens, but I double check and it was broken indeed, so can you download it again from the same link again and tell me if that works better. I have refactored it a bit for more simplicity as well ( one less fsm).

bye,

 Jean

Andys

  • Playmaker Newbie
  • *
  • Posts: 30
Re: RayCast From GameObject
« Reply #14 on: June 17, 2013, 07:27:24 AM »
Hi Jean,
package i have just downloaded is the same as before, still has the same problem

link is from your dropbox folder on post 14th December

Andy