playMaker

Author Topic: Detect the side where a raycast hists  (Read 4347 times)

PET

  • Full Member
  • ***
  • Posts: 139
    • GamerPET
Detect the side where a raycast hists
« on: May 12, 2016, 09:26:21 AM »
Hello there,

I'm not sure how to put this and what to search for exactly... so here it is.

I have a door model that has a colider and a Mouse Pick Event. When I click, the door rotates (opens). The action is the same... it basically always rotates on the same spot... so if I am to open the door from the other side, the door opens towards me. I'm curious how to make the door to always open away from you.

How do you approach this? In theory I could have like 2 colliders and check which one I hit but there has to be an easier way.

Thank You <3
Technical Game Designer
www.GamerPET.com

jameskyle

  • Playmaker Newbie
  • *
  • Posts: 15
Re: Detect the side where a raycast hists
« Reply #1 on: May 12, 2016, 09:55:33 AM »
I'm guessing that the only other way to do this would be to treat the door like a plane and calculate which side of the plane the player or camera is on. As I generally don't like having to work out the maths and trigonometry of these things myself I'd go with two colliders unless there was an action already available.

I'd love to know what someone with more Playmaker know-how would recommend.

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7616
    • jinxtergames
Re: Detect the side where a raycast hists
« Reply #2 on: May 12, 2016, 11:38:17 AM »
Hi,
2 colliders can work.

Another way you could do this is,
when triggering the door, get the x postion (or y depending on the door position)
from the player and check the position if it is lesser or greater than the door X position.

this would be 'cheaper' than 2 triggers as it only needs to check the position when triggering the door and a collider/trigger works every frame.

PET

  • Full Member
  • ***
  • Posts: 139
    • GamerPET
Re: Detect the side where a raycast hists
« Reply #3 on: May 12, 2016, 01:48:53 PM »
Hmm... this is a good point. I will try it tomorrow.

Thanks
Technical Game Designer
www.GamerPET.com

mdotstrange

  • Hero Member
  • *****
  • Posts: 555
    • Can't code? Who cares! Make games anyway!
Re: Detect the side where a raycast hists
« Reply #4 on: May 12, 2016, 03:13:40 PM »
You can use the hitNormal the raycast provides to figure out which way the door is facing and open it in the correction direction based on that-
Indie game dev and instructor at the Strange School. Learn Playmaker at the Strange School!

PET

  • Full Member
  • ***
  • Posts: 139
    • GamerPET
Re: Detect the side where a raycast hists
« Reply #5 on: May 13, 2016, 04:24:06 AM »
Hi,
2 colliders can work.

Another way you could do this is,
when triggering the door, get the x postion (or y depending on the door position)
from the player and check the position if it is lesser or greater than the door X position.

this would be 'cheaper' than 2 triggers as it only needs to check the position when triggering the door and a collider/trigger works every frame.

I almost did it.



The door basically opens as it should. My problem is when the door closes because I can't tell it to just "Add" this to your rotation, because that number might be positive or negative... depending on how the door opened.

So I thought ... Let's do an Initialize state where I just get the Door Position, and I tell the door to just "Rotate To" that position when it closes. Well...
It closes like this:


No idea why.

My second problem is with the X or Y. Right now I only check the X. I don't know how to check the Y in a beautiful way. Like how do I check if the door is like this _ or like this |
Technical Game Designer
www.GamerPET.com

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7616
    • jinxtergames
Re: Detect the side where a raycast hists
« Reply #6 on: May 13, 2016, 06:00:03 AM »
Hi,
To close you can just use a bool test and set a bool when opening the door.

in 'open door' state set bool to true
in 'open door 2' state set bool to false
then in 'close door' do a bool test and make 2 new states to close left side and right side.

i hope this helped

PET

  • Full Member
  • ***
  • Posts: 139
    • GamerPET
Re: Detect the side where a raycast hists
« Reply #7 on: May 13, 2016, 06:04:00 AM »
Yea that could work. But shoudn't my solution work also? I mean I know the original location of the door and I'm storing it as a Vector 3. I then tell it to go to that original position, shoudn't it be enough?


Any idea on the X & Y problem?
I could make another Bool and set it up in the Inspector. Something like "DoorIsOnX" or "DoorIsOnY" and then the person who places the door in the scene set's up if the door is on X or Y... and then when I do the check I check if the door is on X or Y.
« Last Edit: May 13, 2016, 08:51:30 AM by PET »
Technical Game Designer
www.GamerPET.com

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7616
    • jinxtergames
Re: Detect the side where a raycast hists
« Reply #8 on: May 13, 2016, 11:49:00 AM »
Hi,
I found a better way and made a quick video of it, sorry no sound (in a crouwded place)


PET

  • Full Member
  • ***
  • Posts: 139
    • GamerPET
Re: Detect the side where a raycast hists
« Reply #9 on: May 20, 2016, 04:35:49 AM »
Thank! The Angle thing did the job!

It ended up being like this:
Technical Game Designer
www.GamerPET.com