Forum Discussion
nigrin
12 years agoExplorer
[TIP] Detecting where the player looks at
Hi guys, we came across this for our vrjam game "Sorcerer's Wrath", where the player fights things by just looking at them. Here is a quick blog post how we did this, I have not seen this specif...
JoffreyVR
11 years agoHonored Guest
Hi, i've written a similar script, add some collider on the object I want to look at. But now, maybe a really stupid question: Until now, all the scripts i've written had to be add on an object. So my question is, where have I to put this script to make it works ?
Thanks for your future answers.
Cheers,
Joffrey.
Hi Nigrin,
Thanks for sharing that. I tackled the same problem and decided to use the Physics.Raycast() approach. Your approach may be much faster I don't know, I've heard raycasting is slow, but in my situation it didn't matter and I've not seen any speed impact.
I'm pretty new to Unity so my approach is probably poor but I thought I'd share it (written in C#) :-public class PlayerLookingAt : MonoBehaviour {
Transform cameraTransform = null;
void Awake() {
cameraTransform = GameObject.FindWithTag("MainCamera").transform;
}
void Update() {
float length = 10.0f;
RaycastHit hit;
Vector3 rayDirection = cameraTransform.TransformDirection(Vector3.forward);
Vector3 rayStart = cameraTransform.position + rayDirection; // Start the ray away from the player to avoid hitting itself
Debug.DrawRay(rayStart, rayDirection * length, Color.green);
if (Physics.Raycast(rayStart, rayDirection, out hit, length)) {
if (hit.collider.tag == "Tower") {
// Do stuff
}
}
}
}
That's the guts of the code. I've tagged the OVR camera controller as 'MainCamera' and put a collider with isTrigger ticked on the objects I want to know if the player is looking at.
I hope that helps someone,
Cheers,
Mark
Thanks for your future answers.
Cheers,
Joffrey.
Quick Links
- Horizon Developer Support
- Quest User Forums
- Troubleshooting Forum for problems with a game or app
- Quest Support for problems with your device
Other Meta Support
Related Content
- 2 years ago
- 9 months ago