- Home /
Angle between character and ground (raycasthit2d)
Hello/bonjour everyone!
after couple hours trying to calculate the angle between my character and the ground, i asking for help because i'm stuck!
I want my raycasthit2D to analyse if it touch the groundlayer
If it does touch the ground, then calculate the angle between the raycast and the normal of the ground
change the transform rotation to the normal of the ground
//TO LIMIT THE Z ROTATION RaycastHit2D footAngleHit = Physics2D.Raycast(transform.position, Vector2.down,layer); if(footAngleHit.collider !=null) { //give a new angle //print angle }
Answer by Clevereen · May 21, 2018 at 11:40 AM
Hello,
thanks for your help, but it seems i'm getting an error with the normal angle. I'm not getting the normal angle of the ground. Do you know why? (red is my raycast and blue should be the normal of the ground where the raycast hits)
RaycastHit2D footAngleHit = Physics2D.Raycast(transform.position, Vector2.down);
Debug.DrawRay(footAngleHit.point, (Vector2.down) * 5.0f, Color.red);//The original raycast to analyse
Debug.DrawRay(footAngleHit.point, Vector2.Reflect(footAngleHit.point,footAngleHit.normal), Color.blue);//normal raycast angle of collided ground
Probably because the raycast hit on the player box collider (note that I deleted the layer tag when I created the RaycastHit2D)
Another thing: Why did you used Vector2.Reflect? If you want to see the Normal Vector of the ground you must use: Debug.DrawRay(footAngleHit.point, footAngleHit.normal, Color.blue);
hello i'm using it because i've already tried Debug.DrawRay(footAngleHit.point, footAngleHit.normal, Color.blue);
i've been testing and it is true that the raycast hits the box collider of the player ins$$anonymous$$d of the ground. I think it's because the ray starts at the center of the box collider, then hits the edge of the box collider before the ground edge collider.(the script is attached to the player).
i have to figure it out how to start the ray from the feet ins$$anonymous$$d.
i'll try stuffs before asking another question.
Then simply write: RaycastHit2D footAngleHit = Physics2D.Raycast(transform.position - new Vector3(0, 1.1f), Vector2.down); Check if 1.1f is enough (it must be a bit more than the half of the object height).
For those who are trying the same thing here is the full code.
//RAYCAST TO ANALYSE
RaycastHit2D footAngleHit = Physics2D.Raycast(transform.position-new Vector3(0,1.8f), Vector2.down);//the start point is the transform position. Because the start point is the middle of the character, we remove 1.7 on y axis to start the raycast on the edge of the feet
Debug.DrawRay(footAngleHit.point, (Vector2.down) * 3f, Color.red);//The original raycast to analyse
Debug.DrawRay(transform.position - new Vector3(0, 1.7f), -footAngleHit.normal, Color.blue);//normal raycast angle of collided ground
//TO LI$$anonymous$$IT THE Z ROTATION
if (footAngleHit.collider != null)
{
Transform objectHit = footAngleHit.transform;//to get the name of the collided gO
print("Touch the ground : " + objectHit.parent.name);
normalVector = footAngleHit.normal; //Get the normal value
angle = Vector2.Angle(Vector2.down, normalVector)+180f;
if(normalVector.x<0 || normalVector.y<0)
{
angle = -angle;
}
print("angle between vector2 down and footAngleHit : " + angle );
transform.rotation = Quaternion.Euler(0, 0, angle);
}
Answer by MT369MT · May 21, 2018 at 09:57 AM
Hi, Try this script:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Raycast : MonoBehaviour {
public Vector2 NormalVector;
public float Angle;
void Start () {
}
void Update () {
RaycastHit2D footAngleHit = Physics2D.Raycast(transform.position, Vector2.down);
if (footAngleHit.collider != null)
{
NormalVector = footAngleHit.normal;
Angle = Vector2.Angle(Vector2.down, NormalVector) + 180;
if (NormalVector.x < 0 || NormalVector.y < 0)
{
Angle = -Angle;
}
transform.rotation = Quaternion.Euler(0, 0, Angle);
}
else
{
transform.rotation = Quaternion.Euler(0, 0, 0);
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612163115im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Using Player Rotation Quaternion to Make a Directional Vector 1 Answer
Manual Quaternions 3 Answers
Child versus Parent rotations 3 Answers
3D nested Turret Prefab Rotation 1 Answer
Simple Rotation 1 Answer