I’ve tried different sollutions on how I can be able to pick up stuff in my game, but I just don’t really get it at this point.
TO understand what I’m about to say, it’s best to look at this Screenshot form the game:
Say, I wanna have a key lying on a table, and I want my character to be able to “pick it up” when the white Dot (representing the middle of the screen) is over the object, and you are close enough to the object and use the left mouse button. (At this point, it is enough to just destroy the game object when these conditions are met).
Someone told me something about using Raytrace, but I’m all new to this scripting, so if anyone could maybe make me a script that would work, or give me a bunch of info on the subject it would be great!
First,Attach a Trigger Box to the key…
Attach a script named of your choice to the character
var ray;
var hit : RaycastHit;
//add this to update function in the script
if(Input.GetMouseButtonDown(0)) {
ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, hit, 500)) { //500 is the distance between the character and the key.......
if(hit.collider.name == "Key") //the key name in Unity Editor
//Do the thing you want here!
}
}
this is only a sample,for more reference search raycast in unity scripting reference…
What does pick up mean? You could change the parent of the key to the player, so it goes wherever he goes. You could also, if picked up key, that you can instruct a function that the key is now in your possesion. With that info stored, your player can open doors since hasKey == true.