- Home /
Question by
GruzcaClub · Nov 07, 2016 at 03:02 PM ·
unity 5javascriptraycastaislope
How, from scratch, can I detect the angle of the ground underfoot a character not controlled by a character controller.
So me and a team are making a game, and I have been assigned to make sure that the characters cant walk up any cliffsides, so I have been working with raycasts. I have found them weird and don't understand them fully. Please help, thank you.
Comment
Answer by ElijahShadbolt · Nov 09, 2016 at 08:34 PM
RaycastGround.cs
using UnityEngine;
public class RaycastGround : MonoBehaviour
{
public float distance = 1.0f; // distance to raycast downwards (i.e. between transform.position and bottom of object)
public LayerMask hitMask; // which layers to raycast against
void Update()
{
Ray ray = new Ray(transform.position, Vector3.down);
RaycastHit hit;
Debug.DrawRay(ray.origin, ray.direction * distance, Color.red);
if (Physics.Raycast(ray, out hit, distance, hitMask))
{
Debug.Log("Hit collider " + hit.collider + ", at " + hit.point + ", normal " + hit.normal);
Debug.DrawRay(hit.point, hit.normal * 2f, Color.blue);
float angle = Vector3.Angle(hit.normal, Vector3.up);
Debug.Log("angle " + angle);
//if (angle > 30)...
}
else // is not colliding
{
}
}
}
Your answer
Follow this Question
Related Questions
Collision detection with raycast 1 Answer
Why UI layer is being ignored by the raycast? 0 Answers
Raycast Pause Between Hits? 0 Answers
Switch GameObjects Tags with javascript 1 Answer
javascript drops over 30 fps 0 Answers