playMaker

Author Topic: Raycasting when Mouse button held  (Read 2634 times)

Daltone

  • Playmaker Newbie
  • *
  • Posts: 4
Raycasting when Mouse button held
« on: September 03, 2014, 12:47:44 PM »
Hi,

I've had Playmaker for a couple of weeks now and have fumbled through a couple of the tutorials etc.

I've been trying to experiment with raycasting, basically just for my own education more than anything else. I've kind of blundered in to this and got stuck on something that is probably very, very basic.

I've got a scene with a character and a cube. The cube is off the ground and "use gravity" is unchecked.

I've got the "Raycast from Game Object" script from these forums.

The aim is pretty simple. Hold down the right mouse button, click the left mouse button, raycast, check that I have hit a tag called cube, turn gravity on on the object I have hit, causing it to drop to the ground.

This sort of works. When I click I get the raycast, which hits the environment, checks it has hit a cube and turns on gravity. The problem is that either the raycast keeps on going, even when the buttons aren't pushed, then I hit loop count exceeded 1000.

Where the raycast hits the cube Gravity comes on and it drops. Where the raycast hits something else, nothing happens.

What I have is....

Start ---

Get LMB down (store as Bool "Left")
Get RMB down (store as Bool "Right")
Bool all true ("Left" and "Right") send event Raycast

Raycast ---

Raycast from game object forward 100.
HitEvent is blank
DidHit is "DidHit"
HitObject is "HitObject"

Bool if DidHit is true then go to Cube Check
       if Didhit is false then go to Finished and back to the start

Cube Check ---

Game Object Compare Tag "Cube"
if true then go to Gravity
if false then go to Finished and back to the start

Gravity ---

Set "HitObject" to use gravity
then go to Finished and back to the start.

Any help, or an example, would be much appreciated!





Lane

  • Administrator
  • Hero Member
  • *****
  • Posts: 2511
  • Mender of the past
    • Cleverous
Re: Raycasting when Mouse button held
« Reply #1 on: September 03, 2014, 01:05:10 PM »
Put the Cube on a layer and use that layer in your Raycast Layer Mask. Then you can remove the Cube Check state and go straight to Gravity since you already know you hit a cube and you can also change the Raycast state to just go back on Finished and set your Repeat to fire once.

Are you using Get Mouse Button or Get Mouse Button Down?

Get Mouse Button stores a bool constantly, Get Mouse Button Down only trips if you actually press the button down from the up position while in that state which is safer for your situation because Get Mouse Button would enter the state, immediately update the bool, and start firing events because the entire loop can happen before you have time to let go of the mouse button, even 1000 times! =)

If you are firing an event from something, try to use a custom one instead of FINISHED, its a system event for when everything in the state is done and while it works fine most of the time if you ever run into bugs you want to make sure it isnt accidentally firing it instead of a custom event. You'll have more control this way. Only use FINISHED if you want it to fire when every action has literally finished its process completely.

So theres a couple of ways to reduce potential loops and hazards. Give it a whirl and lets see how it goes.
Products by Cleverous
|| Vault Core : Database
|| Vault Inventory : Multiplayer Inventory
|| Vault Attributes : Character Stats
|| That Hurt! : Dmg Floaties
|| Quinn : 3D

Daltone

  • Playmaker Newbie
  • *
  • Posts: 4
Re: Raycasting when Mouse button held
« Reply #2 on: September 03, 2014, 01:53:56 PM »
Thanks for the help.

I did have a rogue Get Mouse Button rather than Get Mouse Button Down at the start (serves me right for being half asleep). Changed all the "Finished" (bar the last one) to something more appropriate too.

A new problem has arisen though. I click and hold the RMB and watching playmaker see the bool flick to "True" then very quickly back to "False". Same thing happens when I click with the LMB, but most of the time it looks like it stays "False". If I click wildly for about 20 seconds I can get them both to flick to true and it looks like it all works ok.

Any ideas?

EDIT:

Just seen this pop up at the bottom of the screen with a red exclamation mark -

state.Fsm != ActiveFsm
UnityEngine.Debug:LogError(Object)
HutongGames.PlayMakerEditor.FsmSelection:SelectState(FsmState, Boolean, Boolean)
HutongGames.PlayMakerEditor.FsmGraphView:HandleMouseDown()
HutongGames.PlayMakerEditor.FsmGraphView:HandleMouseEvents()
HutongGames.PlayMakerEditor.FsmGraphView:DoCanvasView()
HutongGames.PlayMakerEditor.FsmGraphView:OnGUI(Rect)
HutongGames.PlayMakerEditor.FsmEditor:OnGUI()
FsmEditorWindow:DoGUI() (at Assets/PlayMaker/Editor/FsmEditorWindow.cs:94)
HutongGames.PlayMakerEditor.BaseEditorWindow:OnGUI()
UnityEditor.DockArea:OnGUI()

« Last Edit: September 03, 2014, 01:55:51 PM by Daltone »

Lane

  • Administrator
  • Hero Member
  • *****
  • Posts: 2511
  • Mender of the past
    • Cleverous
Re: Raycasting when Mouse button held
« Reply #3 on: September 03, 2014, 02:15:08 PM »
Thanks for the help.

I did have a rogue Get Mouse Button rather than Get Mouse Button Down at the start (serves me right for being half asleep). Changed all the "Finished" (bar the last one) to something more appropriate too.

Nice, should be easier to read and debug now. Both actions have specific purposes, and generally one fits better than the other in each situation.

Quote
A new problem has arisen though. I click and hold the RMB and watching playmaker see the bool flick to "True" then very quickly back to "False". Same thing happens when I click with the LMB, but most of the time it looks like it stays "False". If I click wildly for about 20 seconds I can get them both to flick to true and it looks like it all works ok.

Weird, maybe try something like the image attached. The difference is RMB is on a bool so if its ever released then it goes back to idling. Left click is on a straight up click so it should not loop if it returns super fast.

Quote
EDIT:

Just seen this pop up at the bottom of the screen with a red exclamation mark -

state.Fsm != ActiveFsm
UnityEngine.Debug:LogError(Object)
HutongGames.PlayMakerEditor.FsmSelection:SelectState(FsmState, Boolean, Boolean)
HutongGames.PlayMakerEditor.FsmGraphView:HandleMouseDown()
HutongGames.PlayMakerEditor.FsmGraphView:HandleMouseEvents()
HutongGames.PlayMakerEditor.FsmGraphView:DoCanvasView()
HutongGames.PlayMakerEditor.FsmGraphView:OnGUI(Rect)
HutongGames.PlayMakerEditor.FsmEditor:OnGUI()
FsmEditorWindow:DoGUI() (at Assets/PlayMaker/Editor/FsmEditorWindow.cs:94)
HutongGames.PlayMakerEditor.BaseEditorWindow:OnGUI()
UnityEditor.DockArea:OnGUI()

These creep up sometimes lately with Unity updates flooding through. You can file a bug report if you like, I'm sure Alex would like to at least confirm it is something he is tracking.
Products by Cleverous
|| Vault Core : Database
|| Vault Inventory : Multiplayer Inventory
|| Vault Attributes : Character Stats
|| That Hurt! : Dmg Floaties
|| Quinn : 3D

Daltone

  • Playmaker Newbie
  • *
  • Posts: 4
Re: Raycasting when Mouse button held
« Reply #4 on: September 03, 2014, 03:39:50 PM »
Thanks for all the help!

I've simplified it a bit so that on the "Wait for LMB" state there is just Get Mouse Down for the LMB and Get Mouse Up returns to the "Look for RMB" state.

Seems to work ok!