I’ve had loads of trouble with trying to find this on the internet, I’ve scoured youtube and the forums here but there doesn’t seem to be this type of interaction using raycasting. What i want to achieve is for my character in my game to be able to, for example, look at a door then press the action button (E) to open it, or perhaps to click something to make a message appear on screen.
I think its raycasting I need to use, but if I’m wrong please say; I’m also very new to javascript and C# so I’m unable to adapt the other raycast tutorials I’ve seen. Thanks
There’s a few ways you could accomplish what you are looking for… a few options (depending on your character’s viewpoint) would be to:
-
Raycast directly along the player’s line of sight within a certain “range” (defined by you as the max distance you want items to be able to be activated… so things have to be close rather than all the way across the world etc). When you use the raycast (See: Unity - Scripting API: Physics.Raycast) to detect objects directly in view, you could pass a message to them when the player hits the action button (See: SendMessage()) which would allow you to “activate” the item… all of this would have to be coded of course.
-
You could perhaps use trigger colliders to define “areas” that the player is able to use the action key in. If the player uses the action key while within the trigger, the trigger can pass this along to any given object to active it via script. (again – SendMessage() would be helpful).
If you give us some more details about your game setup and what sort of code you have going atm, more detailed help could be given.
Good luck!
Ill point you in the right direction: FPS1.30 Opening and Closing Doors. Unity3D FPS Game Design Tutorial. - YouTube
Also make sure to see his other play lists, like the Unity basics series, they are really good for getting started fast in unity.
And try to Google for: unity3d fps open door raycast
If you have a specific problem, please do ask a new question