Unity: How to check if player is looking at object?
13 replies, posted
Hey, in Unity scripting, how do I check if player is looking straight at the object? (in my case it's a pic hanging on wall) (it's a 3D model)
How do I do that? Not just when it appears in view, but when directly points at it.
Help please?
[QUOTE=Duskling;38620418]Raycasting.
[url]http://docs.unity3d.com/Documentation/ScriptReference/Physics.Raycast.html[/url][/QUOTE]
I don't get it, how do I make it appear from players view? I don't quite understand how to refer to players eyes/view?
Attach the raycast script to a null object positioned at the player's face.
[QUOTE=arleitiss;38624046]I don't get it, how do I make it appear from players view? I don't quite understand how to refer to players eyes/view?[/QUOTE]
Well, what you could do is get the angle and position of the camera, and use that in a vector which you'll use to tell the raycast which way to shoot.
What language are you writing in?
[editline]28th November 2012[/editline]
Oh yeah, I'm assuming it's a first person game, otherwise that won't work.
Yeah I assumed you didn't mean through the camera, but through the actual model
I am using C# and yeah it's FPS game.
Alright, then you want to make a script and attach it to the camera. I assume you're new to raycasts, so the code looks something like this. I wrote it in this message so I can't guarantee it'll run.
[CODE]
public RaycastHit raycastHit; //Variable which contains information of the gameobject the raycast hits.
if(Physics.Raycast(transform.position, Vector3.forward, out raycastHit, 1000.0f)) //Sends out a really long raycast from the camera, and returns information to the variable we created. You can now use it to do something like.
{
Collider raycastHitCollider = raycastHit.Collider; //We defined a new variable and made it equal to the collider of whatever our raycast hit. We could also make an else statement with a debug.log like so.
}
else
{
Debug.Log("Yo dude, we didn't hit anything!"); //Since our raycast returns false if it doesn't hit anything, we can also create an else statement. This will print out a console log stating "Yo dude, we didn't hit anything!".
}
[/CODE]
Hope this helped you out :)
[editline]28th November 2012[/editline]
Looks a bit messy, but I thought I'd add the comments so you could understand how the code actually works :)
I did it, thanks. I read some tutorials online, not as hard as I thought.
One problem though: Why it can't detect models with small colliders? like phone on table - player can't detect it it just doesn't pick it up. But if I resize it to behalf of the room it does see it.
[QUOTE=arleitiss;38636170]I did it, thanks. I read some tutorials online, not as hard as I thought.
One problem though: Why it can't detect models with small colliders? like phone on table - player can't detect it it just doesn't pick it up. But if I resize it to behalf of the room it does see it.[/QUOTE]
Hmmm, I'm not sure. Maybe the phone is within another collider, or maybe you're just not hitting the phone due to it's size?
Feel free to stop by #unity3d on freenode, I'm sure there's someone there who can help. irc://freenode/unity3d
Well it still doesn't detect phone :L
Any ideas?
[QUOTE=arleitiss;38640465]Well it still doesn't detect phone :L
Any ideas?[/QUOTE]
It's hard, if not impossible, to fix bugs in your game from hearing very little about it. Double check that everything is set up correctly, I can't really help you further than that.
[QUOTE=Doom;38641815]It's hard, if not impossible, to fix bugs in your game from hearing very little about it. Double check that everything is set up correctly, I can't really help you further than that.[/QUOTE]
I decided I will make an workaround.
Get players position, and players rotation. Then make sure if player is standing near phone and is rotated specific way only then to activate it. For now I don't need a perfect function
Sorry, you need to Log In to post a reply to this thread.