playMaker

Author Topic: Raycast help for picking up items  (Read 8428 times)

sorensilk

  • Playmaker Newbie
  • *
  • Posts: 10
Raycast help for picking up items
« on: June 04, 2014, 07:17:04 PM »
TLDR - How to pick up items like in Gone Home?

Sorry this is so long, just trying to give everyone enough info.  I need a bit of help with raycasting. I've been searching for a really long time and haven't found any answers but please let me know if I missed them. Essentially I want to create item interaction like in Gone Home. This is how it works in the game:
  • If the player is close to an item and looking at it, text pops on screen saying something like "Pick up"
  • If they look away the text goes away
  • While looking at an item they can pick it up with 'E'
  • This holds it in front of the camera until they hit 'E' again which will toss the item a few feet.
  • While holding an item they can rotate it by dragging right mouse

Ideally this is done as a FSM only on the Character Controller or a template that I can just attach to any object without having to customize. This is my current setup, screenshots too:
  • First Person Controller
  • GUI Text Manager that I created, creates text on screen or sets it to blank
  • FSM is on the main camera
  • Main camera has an empty game object about 2 feet in front of camera (this is where I want the object to move to.)
  • All items that can be picked up are on the 'Interactive' layer.
  • State 1 is the Raycaster - has a Hit Event to go to State 2
  • State 1 stores the hit object in as a variable (does this need to be global?)
  • State 1 has a filter to only hit objects on my 'Interactive' layer.
  • State 2 sends an event to my GUI text manager and creates the text
  • State 2 has a Get Key Down action for 'E' to go to State 3
  • State 3 has an iTween move to action that should move the object to the empty game object in front of the camera

State 1 works. The variable works (it stores the object hit), my GUI text manager creates the text. Pressing 'E' goes to State 3. Then I start running into problems. Since all my objects need rigidbodies gravity takes affect as it's moving towards the camera and it falls to the ground. Objects that don't have rigidbodies move fine but then stay at that exact world vector and don't move as the player continues to move.

Questions:
How do I reset the Raycast/GUI text if the player looks at an item then looks away? I tried doing another Raycast in State 2 with an inverted mask but that creates an infinite loop.
How do I move the hit object to the empty game object in front of the camera and keep it there until the player presses 'E' again?
How do I rotate a held object when the player right clicks and drags? One axis is fine.

Thanks for any help. ;D

sorensilk

  • Playmaker Newbie
  • *
  • Posts: 10
Re: Raycast help for picking up items
« Reply #1 on: June 09, 2014, 12:39:58 PM »
Any ideas? The most important part is the raycast when the player looks at an item then looks away without picking it up.

Ooghe

  • Playmaker Newbie
  • *
  • Posts: 4
Re: Raycast help for picking up items
« Reply #2 on: June 09, 2014, 02:59:26 PM »
Hi Sorensilk,

I'm very new to Unity and Playmaker, so I don't know how much I can help- but I am trying to figure out the same "Gone Home" like object pick up as you- so I'm happy to compare notes, at least.

I'm approaching the issue a little differently than you are, in that I'm starting with your State 3 issues, and I'm later going to go back into the GUI Text and the ability to rotate/examine.

I've made some meager headway, and right now I'm trying to sort out the physics of selecting an object that has a rigid body, is brought to a child object of the Main Camera, and then is able to be dropped.

The first main difference in my approach is that I'm not putting a raycasting script on my character controller. I'm starting with a Mouse Pick Event on the object itself, so I don't need to bother with putting objects on an interactive layer or any variable of what got hit. I'm assuming if I succeed I'll make it into a template, since I never have to specifically name my target GameObject other than the standard "Use Owner" option.  I have an FSM with 4 states- NotPickedUp, PickingUp, PickedUp, and Dropping.

Second I just discovered, there is a handy function called "Move Towards" (and also "Rotate"), where after the Mouse Pick Event, I set the Parent to the Main Camera and then Move the game object toward my target, which is myself, the Main Camera. I've successfully been able to lug around test objects this way. When I'm ready to drop it, I just reset the Parent to whatever and there it hangs, floating in the air.

Soo, this is where I'm getting an education on physics, gravity and colliders, which I think are pretty key to putting a bunch of objects all over the place but being able to examine them.

The object to pick up could be inside furniture, so I have a dresser with a drawer you open to get a book object, making my target object a child of a dresser drawer. The object, if it's to be thrown around, will need a rigidbody, that uses gravity and keeps kinematics set to off (I found it counterintuitive that turning off kinematics puts something into a ragdoll state, but I learned that it means the gameobject is *not* being controlled by some other script that dictates it's physics)

My first lesson was that Gone Home must live in a world of meticulously created box, not mesh, colliders, though luckily my dresser drawer can use both.  I need the mesh on my dresser drawer in order to have it be openable via a Mouse Pick Event, yet an object must be detectable within. Without a box collider, though, an object with gravity on it plummets down to my terrain, so I had to make a box collider just for the baseboard of the dresser drawer.  Once I put gravity on my object, I had to tweak the size of the colliders to get them to visually look right since the colliders seem somehow imprecise. And honestly, while I'm able to set my book on top of the dresser fine, an object with gravity in the drawer bounces around like there's a poultergeist jiggling it around.  It doesn't do this when I set it on top of the dresser. A possible issue might be my mode of Collision detection (Discrete vs. Continuous)

So, where this is leading is that both gravity and kinematics are controllable as actions within my PickUp Object FSM. When I pick up my gravity, non-kinematic book, I have an action "Use Gravity" that switches off the gravity, and another action "Set Is Kinematic" which switches Kinematics on. If I don't do that, my book object after the Move Toward is bouncing around my Main Camera instead of being stationary in front of me.

My current problem, though, is that when I do it this way, the Book Object seems to impact itself on me in a way that sends me reeling backwards, and my charachter needs to have Kinematics off to move around. So, not quite sure what to do about this yet.

Anyway, that's pretty much where I'm at and I'm also working on my Drop state, where I think you parent your Object to whatever is not your Main Camera, after having done a "Get Position" of where you are when you drop it, and then a Set Position action of the object your dropping, while turning Gravity back on and Kinematics back off.  Probably an iTween Punch or something after that to kick it away.

Finally, I'll also note that I think there's one more state to be considered- rotate object- which I haven't even gotten to.  If you notice, in GH, you first pick up an object, then you can move it to another examination point with the 'E' KeyDown and rotate it, so after I figure out all these physics issues I'll add in another state I expect.

I'm happy to supply screenshots, if you want- but again, I am really new so there may be people on here already talking about Skyrim-style pick up FSMs and so on. I just got my account here today, so I'll look around- but otherwise, looks like it may just be us so far, into that styl of pickup.

best!







sorensilk

  • Playmaker Newbie
  • *
  • Posts: 10
Re: Raycast help for picking up items
« Reply #3 on: June 10, 2014, 07:29:53 PM »
Hey Ooghe,

Thanks for the response. I may have found a way to get the raycasting portion to work. I'm still having a bit of trouble with it but as soon as I figure it out I'll post back here. It should work with bringing objects up to the camera.  Still not sure about rotating objects but that's not a deal killer for me.  What about you?

Ooghe

  • Playmaker Newbie
  • *
  • Posts: 4
Re: Raycast help for picking up items
« Reply #4 on: June 11, 2014, 10:16:07 AM »
I'm basically there, just a few kinks I'm working out. My poultergeist issue was fixed by setting the redundant drawer mesh collider to 'trigger', and I think my first person controller's gravity on my rigidbody wasn't necessary, so the 'impact' of picking up an object is gone.

It turns out there's a PlayMaker action for pretty much every piece of this. We don't even need the empty gameobject two feet from the camera- I got the same result by parenting my selected object to the MainCamera, and doing a Set Position of 0,0,.3.  After it's PickedUp, I can Examine with a KeyDown on E and putting a MouseLook on my selected object.  When I'm done and want to drop it, I reset the parent with an iTween to MoveBy to "throw" it, after giving it gravity.

Outstanding issues- my MouseLook in Examine works great, but my mouse action affects both my object *and* my character controller, so I need to restructure my first person mouselook to be controlled by PlayMaker at the outset so I can turn it off. 

I'd like to get that non-Examine MouseLooking frozen to the center of my screen, like a crosshair, instead of sliding all around (notice that I have a tiny crosshair at screen cetner, but there's always distance between it and my cursor, I'd like those to always align).

Finally, I want to have the ability to "replace object" where it goes back as it was if I am holding the object and then look over it's original position.

Anyway, I made some screenshots which I'll attach to show you my method...
« Last Edit: June 11, 2014, 03:24:08 PM by Ooghe »

Ooghe

  • Playmaker Newbie
  • *
  • Posts: 4
Re: Raycast help for picking up items
« Reply #5 on: June 11, 2014, 10:18:15 AM »
also

sorensilk

  • Playmaker Newbie
  • *
  • Posts: 10
Re: Raycast help for picking up items
« Reply #6 on: June 11, 2014, 06:35:18 PM »
Thanks, Ooghe. I used your method but tweaked it a bit. Images below, but basically this is what I changed:

Picking up State:
Added a Set Rotation action so that the object being picked up faces the camera. You could probably also do this with the iTween Rotate To, Look At, or Smooth Look At actions too. It just depends on how your objects are setup and what kind of visual effect you're going for.

Picked Up State:
I changed the drop transition to Mouse Up, not Mouse Down so the player has to hold down the mouse to hold the item. Just my preference.

Examine State:
I only used the iTween Rotate Add: 180 degrees in 1 second with an ease, Self space. I also added a start event of Finished and unchecked Stop on Exit. So hitting 'E' rotates the item 180 degrees and immediately kicks back to the Picked Up state. It works great, the only weirdness is if you mash the 'E' key over and over it will interrupt the rotation so it may not do an exact 180 rotation.  I'm only planning on the player examining the front or back of objects so this works for me. If you want 360 degree rotation it won't work.

Dropping state:
I just used Set Parent to none, turn kinematic back off, and gravity on. This just drops the item.

My only problem:
For some reason, when I've picked up an item and try to walk straight forward or backwards the character controller gets hung up on something. Strafing left or right works fine. Or if I point the object all the way up at the ceiling or all the way down at the floor the player moves fine. It must be a collider or rigidbody doing something weird but I haven't figured it out yet. Did you ever get that bug?

sorensilk

  • Playmaker Newbie
  • *
  • Posts: 10
Re: Raycast help for picking up items
« Reply #7 on: June 11, 2014, 06:54:37 PM »
Oh, I also wanted to share my GUI Text Manager. I don't know if this will be useful for you but I wanted something a bit more extensive than Setting the GUI text each time. Also helps with accidental spelling errors since everything is in one spot. Or maybe helpful for anyone else reading this thread at some point...

First I created a standard GUI Text which is positioned right above the mouse, blank to start.

Then, I created an empty game object as my GUI Text Manager. On this I have a listener with all my potential text pop ups as global events. Each of these take us to the next state which is a Set GUI Text action with the text I want. There is even a blank one for clearing the text.

Then, when you want to set the text at any point, from any other FSM, instead of doing Set GUI Text each time you just do a Send Event action. Target the Game Object FSM, your GUI Text Manager and you can select the global event from the drop down box. I named all my global events the same as the text so it's nice and easy.

1982

  • Junior Playmaker
  • **
  • Posts: 71
Re: Raycast help for picking up items
« Reply #8 on: June 12, 2014, 06:37:51 AM »
[quote author=sorensilk link=topic=7523.msg36622#msg36622
For some reason, when I've picked up an item and try to walk straight forward or backwards the character controller gets hung up on something. Strafing left or right works fine. Or if I point the object all the way up at the ceiling or all the way down at the floor the player moves fine. It must be a collider or rigidbody doing something weird but I haven't figured it out yet. Did you ever get that bug?
[/quote]

I've had this too, it is because character collider is colliding with the item in hand. You can disable collider of the item, or make it have a layer which does not collide with player layer. Maybe also if the item is pivoted far enough from the character collider it will solve it. Try also adjusting the radius of character collider.

Ooghe

  • Playmaker Newbie
  • *
  • Posts: 4
Re: Raycast help for picking up items
« Reply #9 on: June 12, 2014, 10:13:25 AM »
Thanks for the tweaks, Sorensilk. The rotate action is a good idea, and the GUI Text Manager is super helpful.

For what it's worth, I solved my Examine Mouselook issue yesterday (where rotating my examined object was also affecting my character movement), by putting Send Events on my Examine transition states that control whether my First Person Controller and First Person Cam are in a state of "MouseFreeze" or not (by setting the Mouselook sensitivities on them to 0)

That seemed to do the trick. I like your 180 degree alternative though, that might be all I need. Currently, I'm left with what I think is basically a Math issue, where differently sized pickup objects would need to be held at slightly different distances from the camera if I'm going to rotate them 360 without potentially overlapping with the camera itself so I see inside the polys. If I ever get that worked out I'll repost it here.

I think 1982 above is correct. Your issue sounds very much like what I experienced  with the Book Object impacting itself on me in a way that sent me reeling backwards. I didn't think it was colliders looking at it visually in scene view, but my pickup object has a .3 Z vector and my First Person's collider radius is .3, so yea, it sounds possible. I solved the issue by turning off gravity on my First Person controller, not an ideal solution but not an immediate issue for me.

Right now I'm switching from PlayMaker for awhile to see if I can learn light mapping better, but I expect I'll be back as I get more into global variables.  Thanks again for the tweaks!