I’m setting up a game where the player can interact with various characters in the world - I need a way for the player to ‘click’ or otherwise interact with the game characters. Is there a way to detect where the user is ‘looking’ (when using mouse-look?) or for them to click on the game character? So that I can use that as a trigger to initiate some behavior on the part of the game character?
Any help’d be appreciated! (I’m utterly new to Unity, but am a half decent programmer)
If what you want is to detect when clicking on a game character, you might want to have a look in the Examples folder at the Ragdoll Example. In the example you can click on the Ragdoll and throw him around. It is actually quit fun Should I be worried about my mental state?
Lets see if I can help. The game uses a first person camera, we have other non-playable characters in the game. When the user navigates the game, he will come across these characters, is there a way that when a user looks at a character and clicks, we can trigger an action such as an animation or sound.
I have a few questions for the really smart people on this. Assuming your “click” is any click regardless of where the mouse is:
Could you cast a vector forward from your character, then on each of the other possible characters get their position, then compute the angle between. Accept if the angle is in a predefined range?
When an object is not being rendered by the camera is there someway to determine this? Is the object still active, or is there a visible property some place, or a list of the objects being rendered by the camera, or something?
Just thinking out loud and hopefully not cluttering the thread
Couldn’t you just cast a ray from the camera and see what it hits? If it hits your character then play the animation/sound. I think the thing to look at is ScreenPointToRay in the Screen class? I have never done it but that’s how I figure it should work…
Another thing you could check is the distance between them…
var distance = Vector3.Distance(thePlayer.transform.position, theTarget.transform.position);
if (distance < 10) {/*rotate thePlayer to face theTarget and do your thing*/}