- Home /
C# Check Physics.Raycast Once
I would like to make an if statement that checks if Physics.Raycast only detects a gameobject once instead of checking repeatably. Is there a way to do this? I know that the update function is constantly checking so I don't know how to get around that.
using UnityEngine;
using System.Collections;
public class Example : MonoBehaviour {
void Update() {
Vector3 fwd = transform.TransformDirection(Vector3.forward);
//Check only once
if (Physics.Raycast(transform.position, fwd, 10))
print("There is something in front of the object!");
}
}
Comment
Either call your code from a function that doesn't get called every frame, or keep track of whether the check has been performed or not.
Your answer
![](https://koobas.hobune.stream/wayback/20220613143133im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Finding Distance between Angles and Points 2 Answers
Getting the Furthest Point on a Ray 1 Answer
C# Raycast 2D GameObject follow Mouse 1 Answer
C# Physics.LineCast Argument is Out Range 1 Answer
AI Shooting for Aircraft 1 Answer