- Home /
Question by
pawkwi24 · Feb 09 at 06:43 PM ·
raycastdetectionsensorondrawgizmos
OnDrawGizmos as a drone sensor
Hi, I wrote some code that should detect an element like a cube in front of it. I need this for my drone sensor project. Unfortunately, it only detects on one axis. What should I improve so that each of the OnDrawGizmos show a separate distance to the cube element?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class sensors : MonoBehaviour
{
public GameObject lastHit;
public float rangeCollision;
public Vector3 collision = Vector3.zero;
public float targetVelocity = 10.0f;
public int numberOfRays = 10;
public float angle = 360;
// Update is called once per frame
void Update()
{
var ray = new Ray(this.transform.position, this.transform.forward);
RaycastHit hit;
if(Physics.Raycast(ray, out hit, rangeCollision))
{
lastHit = hit.transform.gameObject;
collision = hit.point;
}
}
void OnDrawGizmos()
{
for(int i = 0; i< numberOfRays; ++i)
{
var rotation = this.transform.rotation;
var rotationMod = Quaternion.AngleAxis((i / (float)numberOfRays) * angle, this.transform.up);
var direction = rotation * rotationMod * Vector3.forward;
Gizmos.DrawRay(this.transform.position, direction);
}
}
Comment
Your answer