- Home /
How I can know surface angle?
Hi, I have to know rotation of surface i'm looking, but I dont know how I should implement it? I'm not searching complete code, but even idea how I could do that?
Like this, arrows are showing surfaces, I need to know rotation of surface i'm looking. I was thinking multiple raycasts, but not sure how to implement that. Any idea?
EDIT:
New picture (I'm bad at describing, sorry!), maybe it helps someone.
^ This raycast should find angle information.
since a RaycastRit
contains a normal you could use that to deter$$anonymous$$e the angle of the face being hit, but these normals are only really useful for "simple" geometries.
How I can access that angle? I'm pretty new with raycasting since I have used it only with really simple things.
Do you mean the normal or the angle around Z axis? Do you mean the angle relative to the connecting edge or an absolute angle? What is the sort of value that you want to derive for the bottom, sloped and right edge for example?
I dont really know why someone added negative vote to my question, I can add more information if it's needed and that's why there is comment box. I didn't find good answer using search since it's not really general question. @Statement ♦♦: I mean normal, object z-axis isn't enought since it's same on every surface. I added new image to first post, maybe it helps.
I am still unclear as to what this will be used for: pathfinding, ricochets, other. what? if it is pathfinding or ricochets there are easier methods then raycasting, but maybe application would help.
Answer by Loius · Jan 09, 2013 at 06:10 PM
A raycast hit contains the ".normal" attribute, which is a Vector3 representing the hit plane's normal (the direction its "outside" face is facing).
You can use Vector3.Angle(normal, yourRay) to get the angle between those, if that's what you're looking for.
You'll need -yourRayDirection
ins$$anonymous$$d of yourRay
, otherwise the value returned is 180°-angle. Also note that smooth surface normals and probably also bumpmaps will modify the normal, and therefore the angle.
Thanks for the answer. I'm probably idiot, but I can't get this working. $$anonymous$$y code is like this now:
var hit : RaycastHit;
var direction = transform.TransformDirection(Vector3.forward);
var ray = transform.position;
var normal: Vector3 = Vector3(hit.normal.x, hit.normal.y, hit.normal.z);
if(Physics.Raycast(ray,direction,hit)){
var test = Vector3.Angle(normal,ray);
Debug.Log(test);
}
As I've written above, you need to use "-direction", not "ray". Also, directly use hit.normal ins$$anonymous$$d of creating a local variable, it's faster. And currently you're assigning "normal" before calling Physics.Raycast, so the value will be incorrect.
Also note that "transform.TransformDirection(Vector3.forward)" is simply "transform.forward", which is also faster.
Answer by hotozi · May 23, 2019 at 08:14 PM
Don't break your head i found the simplest and best answer for calculation... RaycastHit has a transform in which it is linked to the collider once it is called
Rayhit.transform.eulerAngles
''
var Rayhit: RaycastHit;
var HitRay= Physics.Raycast(transform.position,transform.forward,Rayhit,1);
if(HitRay)
print(Rayhit.transform.eulerAngles.y);
''
What does the object rotation on the y axis has to do with the surface angle? Yes, if your object is aligned with the principal axis you can work out the surface angle from the object rotation. However if you have a look at the OPs image the object has several different surfaces at different angles. So your answer is not really simpler nor the best answer. The best answer was already given by Loius over 6 years ago.
Apart from that you should avoid giving example code in UnityScript as it has been deprecated long time ago and is no longer supported in the latest Unity versions.
ps: i removed your second answer on the other similar question. Please do not bump old questions which already have been answered. Especially don't spam old outdated code to somewhat similar questions. Take the time to read the question and answer that question.