I am trying to come up with a way to implement use-key functionality in my side-scrolling game. I was wondering what have other people done in the past to get this functionality. The only way that comes to mind right now is to use raycasting to cast a ray in the Z direction and make sure that each of my “useable” objects have a collider placed behind where the player would be standing but that seems quite laborious for my needs and there has to be a better way.
By the way I am new to the forums and fairly new to Unity. I have been impressed with it so much that I already bought the Pro version 
Hi, welcome to the forum!
How exactly do you determine whether an object is usable in the game? Is it by proximity, the character’s line of sight… ?
I haven’t laid down anything for this functionality yet. The only thing I was planning on doing was maybe give them all a tag of ‘InteractiveObject’. But since I am so early on in the development of my game I figured I would post on the forums before I get myself stuck in an inefficient way of handling object interaction. If you were asking how I envisioned it working then I want it to be based on proximity. For example you would walk in front of a switch object, press the ‘f’ key and have it run whatever method I need it to. Hope that makes enough sense.
You can check the distance between two objects by calling Vector3.Distance on the two positions:-
var distance = Vector3.Distance(player.transform.position, key.transform.position);
Another option might be to use an object with a trigger collider. A trigger is just an ordinary collider with the Is Trigger option enabled. A trigger won’t actually collide with objects, but the OnTriggerEnter function of the object’s script will be called when another object enters the collider’s volume.
Thanks for the help Andeeee. I understand how to get the distance from objects but I was looking to figure out an efficient way to implement the functionality. I guess I can get a list of everything tagged with “InteractiveObject” and send them a trigger message if they are within a certain distance, but to me it seems like there has to be a more efficient way to do it so I don’t have to poll every interactive object in the level. Maybe I am over-thinking it and that solution is perfectly acceptable and will not cause any noticeable lag.
As for the trigger event I would prefer the player intentionally activate the trigger with a keypress instead of stepping into volume. I do plan on using that for other things such as triggering cut scenes.
So what do you think? Should I just go with the poll-every-InteractiveObject method?
There is also an OnTriggerStay function which is called each frame while an object is inside the trigger. In this function, you could check for a keypress to activate the object. If you have a lot of objects, then checking them all for proximity might become a significant overhead - triggers are probably better in such cases.
Hello …
you helped me on another thread, now it’s my turn to help you ggg.
There are basically two ways of doing it.
The first is Physics.OverlapSpherehttp://unity3d.com/support/documentation/ScriptReference/Physics.OverlapSphere.html
where you get all objects which are within the radius you provide. This is not the most performant way,
but more performant than iterating through ALL objects in your scene 
But this is more for searching things, like an ai player that is searching an enemy to attack.
andeeee’s idea should be better.
You simply activate the checkbox “isTrigger” in the rigidbody and then it will report anything that is inside it.
Here is your example code, to be put into the script of your pickable objects: (C# example)
void OnTriggerStay(Collider other)
{
if (other.gameObject == playerobject)
{
if (Input.GetKey(KeyCode.A))
{
PickupCodeHere();
}
}
}
in your finished game you would rather use Input.Getaxis…
Hope that helped.
kind Regards,
David
ps: I’ve also bought Unity Pro very recently and its indeed worth every coin …
But I’m a bit astounished that you could effort to buy it that soon (without ‘really’ knowing how good unity is)
Perhaps we can help out each other a bit with our games ?
Thanks for the responses! I knew there was a better way of handling use-key functionality but for some reason I couldn’t figure it out. I think I will use the OnTriggerStay event. It sounds pretty simple to implement and would save me from having to poll every object in my game.
And I was glad to help you Reculum. I didn’t want to be a person who lurks on the forums, asking questions but never answering so I will help where I can
And I am using Unity to develop my senior project whom I am the project leader. I had my group go out and test out various higher profile game engines and come back with a report on the price of the licence and a features list along with some other details and Unity came on top. I played with the Pro trial, followed some tutorials, saw how powerful and simple it was, and decided to go with it. Plus, it didn’t hurt that it was $300 off 