- Home /
how to pick up and read paper [solution found sorry]
im a BEGINNER in unity and i want to have my player pick up and read a piece of paper but i don't know how to script it so that way you can see the paper on the ground pick it up and it pops up on the screen with written text on it... can someone help me?
Answer by drex150 · Aug 04, 2014 at 08:59 PM
There is a lot you want to happen with this and there's a lot of things you need to do to make it happen.
I'm going to give you a brief rundown of how you'd go about doing it, but I'm not gunna write the code for you because it would be rather large. Instead I'll point you in the right direction.
So first off, you're going to want to place your paper in the scene with your player. You'll want the paper to have a tag associated with it. (http://docs.unity3d.com/Manual/Tags.html)
In order for the player to interact with the paper, you're going to need to use Raycasting that is set to the camera. (http://docs.unity3d.com/ScriptReference/Physics.Raycast.html)
Once the ray touches the paper, sees the tag of the paper says "Paper", and the player is close enough to the paper (distance is automatically calculated in raycasting) you will need to press a button. (http://docs.unity3d.com/ScriptReference/Input.GetButtonDown.html) The button you press will depend on the inputs you set up. To access the inputs go to Edit > Project Settings > Input. (http://docs.unity3d.com/Manual/class-InputManager.html)
Once the button is pressed, if you want the paper to disappear, you need to destroy it. (http://unity3d.com/learn/tutorials/modules/beginner/scripting/destroy) If you want it to stay, you can skip this step.
Now you need to make the object appear in front of the camera. There's lots of ways to go about this. You can move the paper that you just pressed the button on, or you can delete that object and instantiate a new one in front of the camera, or you can use a GUI texture and just enable it when you click that paper. There's probably a lot of other ways to get the same effect too. You'll have to figure out what works best for you.
Now you'd just need to press another button when you are finished looking at the paper to make it go away.
That's the way I'd probably do it myself. So I hope it helps at least get you started.
Answer by AW0610AUT · Aug 04, 2014 at 09:12 PM
If you are a complete beginner it might be better to lern the basics first. But i would (depending on the situation 2d/3d..) make something like this:
private var Player: GameObject;
private var distance: float;
private var open: boolean = false;
privat var someDistance: 10; //this is the max. distance you can be away from the Paper and still read it:
function Update(){
Player = gameObject.FindWithTag ("Player"); // here you find the player in the scene (make sure the player is tagged as "Player")
distance = Vector3.Distance(transform.position, Player.transform.position); // with Vector3.Distance you can get the distance between two positions.(The Vector length)
if (distance < someDistance){ //you check if you are not to far away
open = true; //if you are close enough to read it open will be set to true
}
else
open = false;
}
function OnGUI () { //in the gui you check if open = true and display the message you want to display
if (open){
GUI.Label (Rect (0,0,Screen.width,Screen.height),"This is a Note")
}
}
}
I held it somewhat basic and explained a little bit. (Note im not the best programer out there). Not tested by the way. But again you lern more when you try stuff yourself.
Here are some Links you should check out: http://docs.unity3d.com/ScriptReference/Vector3.Distance.html http://docs.unity3d.com/ScriptReference/GUI.Label.html http://unity3d.com/learn/tutorials/modules/beginner/scripting/variables-and-functions
Haha, no worries. Your answer is really helpful too! The only reason I didn't decide to go the same route was because your code doesn't care if the player is facing the paper. Ins$$anonymous$$d he just has to be close by. I was going more for, you must be looking at the paper in order to interact with it hehe.
Your way is definitely easier though, which is good for a beginner! ^^