playMaker

Author Topic: Quick raycasting question  (Read 974 times)

ultrafop

  • Playmaker Newbie
  • *
  • Posts: 6
    • View Profile
Quick raycasting question
« on: May 05, 2018, 01:54:38 PM »
Hello all,

I think this is a quick question!

So I'm coming from Construct 2, where there really isn't any built-in raycasting (I essentially made what I'm doing in Unity using trig and overlapping checks,), so I'm pretty new to the concept and how to use it best (I'm excited to learn something new!!). I'm wondering, if I'm using raycasting to set an object to the floor's angle, will that require 2 rays (one on each side of the object) or can I do it with one ray? Also, if I need 2 rays and have to find some angle between them, can I use playmaker to do this or would I need to script it myself? Any information is sincerely appreciated!
« Last Edit: May 05, 2018, 02:02:36 PM by ultrafop »

ultrafop

  • Playmaker Newbie
  • *
  • Posts: 6
    • View Profile
Re: Quick raycasting question
« Reply #1 on: May 06, 2018, 02:44:02 PM »
Okay

So I've been searching these forums and trying more on this and realized you can indeed get a normal with one ray. However, I'm working in a 2D project and need a Z axis value to rotate my character. I've tried using a normal RayCast (i.e. not Ray cast 2d) to grab a z value but when I do it saves a euler as vec 0,0,0 no matter what. Oddly enough if I simply grab rotation for an object I can get all three vectors no problem (also, testing a ray cast 2d gives vectors for X and Y just fine). Are either of these outcomes a bug?

Also, assuming I can't get a Z for a normal raycast in a 2D project, can I extrapolate Z using X and Y with a ray cast 2d? I've been trying but maybe I just don't understand how the raycasting works with normals yet. Any help is deeply appreciated!!

ultrafop

  • Playmaker Newbie
  • *
  • Posts: 6
    • View Profile
Re: Quick raycasting question
« Reply #2 on: May 07, 2018, 11:46:47 AM »
bump

I swear I'm trying everyone lol. I need a raycasting shirpa! It's cold on this mountain.

DanielThomas

  • Beta Group
  • Full Member
  • *
  • Posts: 148
    • View Profile
Re: Quick raycasting question
« Reply #3 on: May 07, 2018, 12:56:53 PM »
I think it always help if you add a screenshot if your state. Raycast2D should be able to store the normal. But maybe you using it in a way it was not intended. The normal will return a vector2, so you will need to do math on it to get the actual degrees. Is that what you're looking for?
Maybe this could help:
https://answers.unity.com/questions/638293/what-does-raycasthitnormal-return-exactly.html

ultrafop

  • Playmaker Newbie
  • *
  • Posts: 6
    • View Profile
Re: Quick raycasting question
« Reply #4 on: May 07, 2018, 04:27:32 PM »
I think it always help if you add a screenshot if your state. Raycast2D should be able to store the normal. But maybe you using it in a way it was not intended. The normal will return a vector2, so you will need to do math on it to get the actual degrees. Is that what you're looking for?
Maybe this could help:
https://answers.unity.com/questions/638293/what-does-raycasthitnormal-return-exactly.html

Hey! Thank you for replying! This is helpful but it's focused on Vec3 (I've read through but I don't see a method of extrapolating z from x and y in the post).

Yes! That is exactly what I want to know (How to ge the angle from the points)! Currently I am squaring X and Y and adding them to obtain Z . That hasn't worked and typically leads the player box to rotate in a circle infidelity, even when setting Z to 0 at every frame before adding the values together.

Here is what I'm doing:




EDIT add: The images don't appear to be posting here neatly. Here is a link:
https://imgur.com/a/txm9zTf

I do want to note that I've tried ticking the every frame and late update boxes, they are just unticked in the screenshots.

Thank you for your help!
« Last Edit: May 07, 2018, 04:34:30 PM by ultrafop »

tcmeric

  • Beta Group
  • Hero Member
  • *
  • Posts: 747
    • View Profile
Re: Quick raycasting question
« Reply #5 on: May 07, 2018, 11:29:06 PM »
Hi,

So i helped somebody solve this using vector3s recently. Here was the process. Maybe you can use a similar method.

"Make sure to only raycast the floor. Put it on a seperate layer, etc.
Save the hit normal. This is your first vector3
Then use the transform direction action, to get the transform down in world space. If you dont know what that means, just copy my pic. This is your second angle.
Then use my custom action to get the results of the angle between the two vector3s
180 means perfectly flat."

tcmeric

  • Beta Group
  • Hero Member
  • *
  • Posts: 747
    • View Profile
Re: Quick raycasting question
« Reply #6 on: May 07, 2018, 11:33:12 PM »
Here is a custom action to get the vector2 angle (my example before was using vector3 for 3d space).

From: https://docs.unity3d.com/ScriptReference/Vector2.Angle.html

Hopefully this gets you a step closer.


ultrafop

  • Playmaker Newbie
  • *
  • Posts: 6
    • View Profile
Re: Quick raycasting question
« Reply #7 on: May 07, 2018, 11:46:46 PM »
Thank you!! I really appreciate you time and working on a script! I'll give this a shot!