Playmaker Forum

Playmaker Updates & Downloads => Share New Actions => Topic started by: jeanfabre on December 07, 2011, 02:59:21 AM

Title: RayCast From GameObject
Post by: jeanfabre on December 07, 2011, 02: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
Title: Re: RayCast From GameObject
Post by: jayhfd on December 13, 2011, 04: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
Title: Re: RayCast From GameObject
Post by: jeanfabre on December 14, 2011, 05: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
(https://i.imgur.com/oOIu5Or.png)


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
Title: Re: RayCast From GameObject
Post by: Dev_Sebas on December 14, 2011, 07:46:33 AM
This problem
Store?
Thanks for any help
Title: Re: RayCast From GameObject
Post by: Dev_Sebas on December 14, 2011, 07: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
Title: Re: RayCast From GameObject
Post by: jeanfabre on December 14, 2011, 10:24:04 PM
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
Title: Re: RayCast From GameObject
Post by: Dev_Sebas on December 15, 2011, 02: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
Title: Re: RayCast From GameObject
Post by: jeanfabre on December 15, 2011, 10:39:03 PM
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
Title: Re: RayCast From GameObject
Post by: Dev_Sebas on December 16, 2011, 10:33:15 AM
Thanks a lot Jean ;D finally
PlayMaker should have more tuts
Bye
Title: Re: RayCast From GameObject
Post by: FritsLyn on April 22, 2013, 01: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 (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 ;)
Title: Re: RayCast From GameObject
Post by: Lane on April 22, 2013, 01:31:26 PM
This thread is ancient, I would think a few errors are expected in something that old.
Title: Re: RayCast From GameObject
Post by: jeanfabre on April 23, 2013, 11:42:54 AM
Hi,

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

bye,

 Jean
Title: Re: RayCast From GameObject
Post by: Andys on June 16, 2013, 01: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
Title: Re: RayCast From GameObject
Post by: jeanfabre on June 17, 2013, 03: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
Title: Re: RayCast From GameObject
Post by: Andys on June 17, 2013, 04: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
Title: Re: RayCast From GameObject
Post by: jeanfabre on June 17, 2013, 05:55:10 AM
Hi,

 I changed the name of the file, can you download it again from that post, it may be a caching problem or something.

bye,

 Jean
Title: Re: RayCast From GameObject
Post by: Andys on June 17, 2013, 08:26:44 AM
Hi jean,
Ive tested the new RaycastTest file, and it does the same as before, just sits in the Fire Delay state, and the error is still present in the Get Mouse Button state.

One clicking the mouse what should it be doing? as it does not move from the Fire Delay state.

Andy
Title: Re: RayCast From GameObject
Post by: jeanfabre on June 18, 2013, 11:10:39 PM
Hi,

 import it in a new project, you are still in the old scene.

bye,

 Jean
Title: Re: RayCast From GameObject
Post by: Andys on June 19, 2013, 11:10:24 AM
I have created a new project and it is still the same.

as i said before, on clicking the mouse what is purposed to happen, as i cant see anything

Andy
Title: Re: RayCast From GameObject
Post by: jeanfabre on June 19, 2013, 10:43:27 PM
Hi,

 ok, let's recap, I think I know where the confusion is.

 mouse click will nto fire where you click, it will only update the raycast when you click.

so.

1: open "Laser/firing system" fsm.
2: make sure that "Use Mouse click for firing" is true
3: run the scene
4: move the gun with WASD keys, you will see that the raycast doesn't update,
5: click, the rayscast fires and adjust the red debug rectangle.

Is this better?

bye,

 Jean

Title: Re: RayCast From GameObject
Post by: Andys on June 20, 2013, 01:14:45 PM
Hi Jean,
Tried as you have listed and up to point 4 all ok, but when clicking in scene, nothing updated. Should the bool test in on mouse down have a variable?

Andy
Title: Re: RayCast From GameObject
Post by: jeanfabre on June 20, 2013, 10:28:27 PM
HI,

 ok, I have an issue with drop box it seems... indeed it's still the old package... so I have copied it ( instead of renaming it) to a different location:

try this:

https://dl.dropboxusercontent.com/u/17356625/Unity/playmaker/samples/raycast.unitypackage

bye,

 Jean
Title: Re: RayCast From GameObject
Post by: Andys on June 21, 2013, 04:14:07 AM
Hi Jean,
now working as you explained.

If i am to fire a projectile on mouse click, is the vector 3 variable "look at target" the one to use?

thanks

Andy
Title: Re: RayCast From GameObject
Post by: jeanfabre on June 24, 2013, 12:21:02 AM
Hi,

 Good, sorry for the mess :)

Nop, the look at target is the actual point where the raycast hits.

if you want fire a projectile, you need to fire from the z axis of the gun ( in our case).


For this, use "transform direction" action, reference the gun, and for the local direction (0,0,1), which is the z axis.
 that's your direction to add force to a projectile.

bye,

Jean
Title: Re: RayCast From GameObject
Post by: colpolstudios on August 08, 2014, 02:44:38 PM
Hi, I just wish to confirm that the play maker scene works fine.

Would you mind if I updated this with some minor improvements?

1. Model a new cube and uv map it so we can see the direction we shoot in.

2. Ability to hit and test enemy or wall hit and create particle effect at hit points.

3. Test did we hit nothing and take action

Thank you for the time taken to create the example.

Learned a lot from it:)



Title: Re: RayCast From GameObject
Post by: jeanfabre on August 11, 2014, 06:20:36 AM
Hi,

 Good, that's going to be a nice sample to add to the ecosystem. Can you bump this early september? Unite is getting closer, I don't think I'll have time before.

 Bye,

 Jean
Title: Re: RayCast From GameObject
Post by: colpolstudios on August 15, 2014, 03:02:24 PM
Hi, Jean here is the web player demo.

https://dl.dropboxusercontent.com/u/150707256/Raycast%20to%20shoot%20Demo%20playmaker/Raycast%20to%20shoot%20Demo.html

Updates:

New model with texture so we can see the direction we are aiming in (only a basic example).

Gui on screen instructions.

Spawned particles if you hit either a wall or the enemy.

Hopefully this basic example would help others.

Unity Scene: https://dl.dropboxusercontent.com/u/150707256/Raycast%20to%20shoot%20Demo%20playmaker/Raycast%20to%20shoot%20Demo.unity3d







 
Title: Re: RayCast From GameObject
Post by: jeanfabre on September 18, 2014, 06:16:13 AM
Hi,

 Very good :)

It'll go in for sure. I am still bumping into a major issue with the current beta ecosystem as it crashed Unity when you import a package... and I am still unable to understand where I need to change my code so that it doesn't... as soon as I figured that one, then I can release the new version of the ecosystem which will allow you to browser samples and install them without leaving Unity :)

 Bye,

 Jean

 Bye,

 Jean