- Home /
Single raycast from player that I can use in all other scripts?
My problem, is that I have several objects in my game, that require the player to look at them in order to activate them (in 3D space with a FPP.)
As I see it, I have to create and use a new raycast from the player camera, for every object. Is there a way, where I have a script on my player that only exists to create a ray from the player camera, and then reference that ray, in all my other scripts on the objects, to check wether the player is looking at them?
Answer by leSamo · Jan 03, 2018 at 12:47 PM
Create a script that will be attached to every player. In that script create a public raycastHit hit. In your master script you can get all scripts with:
bool hit = false;
foreach (YourPlayerScript player in FindObjectsOfType<YourPlayerScript>()) {
// your code to check raycasts
if (player.hit.something) {
hit = true;
}
}
Your answer
Follow this Question
Related Questions
unity UI Collision check not working 1 Answer
make the layer "IgnoreRaycast" work with collisions. 0 Answers
OnCollisionEnter2D not triggering fast enough(?) 2 Answers
Collision2D not detecting when playing is jumping 2 Answers
Did triggers inside loaded gameobjects break in a recent update? 0 Answers