Question by
Pibastte · Dec 07, 2017 at 10:23 AM ·
raycastdistanceraycasthitsensor
How to use RaycastHit ?
I want to get the distance between an object and a hit point and so I think I need to use RaycastHit. I tried this code but it doesn't output anything,
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class newSensor : MonoBehaviour {
float dist;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void FixedUpdate()
{
RaycastHit hit;
if (Physics.Raycast(transform.position, Vector3.forward, out hit, Mathf.Infinity))
{
Debug.Log(hit.distance);
}
}
}
Thank you for helping me :)
Comment
If it does not output anything, it means you don't have any collider in your scene or the ray does not hit them. Use Debug.DrawRay
to visualise your ray.
https://docs.unity3d.com/ScriptReference/Debug.DrawRay.html
Debug.DrawRay(transform.position, Vector3.forward * 500, Color.green);
Your answer
Follow this Question
Related Questions
Swipe single object at a time upon touch, not swiping allover the screen 0 Answers
Raycasts stop working after ~3 seconds 1 Answer
player try to go into the ground to reach raycast hit 1 Answer
BulletHole and HitSparks spawning in front of weapon 0 Answers
How to freeze an GameObject on x axis in specific direction? 1 Answer