- Home /
How to make player have the ability to pickup and read a book (like in Skyrim)
I want my player to be able to pickup a book that i make on the ground, and give them the ability to read it, like in skyrim. I have an idea on how to do it with an animation, but im not sure how to pause the game in the background and apply the animation.
Answer by agamedesigner · Aug 03, 2012 at 03:22 AM
Sure you can do that. There are a lot of different ways.
Here is just a simple way.
This will cast a ray from where the mouse cursor is, and if the ray hits something tagged as "Touchable" then it will take the appropriate action.
public class TryTouch : MonoBehaviour
{
GameObject currentlyTouched;
void Touch()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast (ray, out hit, 10f))
{
if(hit.collider.gameObject.tag == "Touchable") // Did it hit the paper?
{
// Show the paper
hit.collider.gameObject.SendMessage("OnTouch");
currentlyTouched = hit.collider.gameObject;
}
}
}
void Update()
{
if(Input.GetMouseButtonDown(0)) // If the player clicks with the left mouse button
{
if(currentlyTouched == null)
{
Touch(); // then execute Touch()
}
else
{
currentlyTouched.SendMessage("UnTouch");
currentlyTouched = null;
}
}
}
}
As far as actually showing the paper, there are lots of things you can do. In this simple case, the above script can be attached to an empty GameObject. The below script is attached to the actual in-game Paper model that you want the person to be able to read. Make sure that the GameObject for your paper has a collider, and set the GameObject's tag to "Touchable" (you'll have to add a new tag).
Once that is done, you want to attach the script below to the paper. Then create a new GUITexture and set it up with your paper image from photoshop. Make it a child of your paper model that will be clickable, and disable it. Then drag it into the displayImage slot of the script on your paper model.
You can of course add functionality so that you can flip through pages who are children of the 3d paper model. But I think I have set out enough of a foundation for you to figure that out.
public class TouchableObject : MonoBehaviour
{
public GameObject displayImage;
void OnTouch()
{
displayImage.SetActiveRecursively(true);
}
void UnTouch()
{
displayImage.SetActiveRecursively(false);
}
}
sorry for the indentation; can't get it to look proper.
Thank you so much for your responds (really infomative), but I am getting a whole bunch of errors:
Assets/Scripts/papertouchablefunction.js(1,30): BCE0043: Unexpected token: :.
Assets/Scripts/papertouchablefunction.js(2,1): BCE0043: Unexpected token: {.
Assets/Scripts/papertouchablefunction.js(3,9): BCE0043: Unexpected token: GUITexture.
Assets/Scripts/papertouchablefunction.js(14,1): BCE0044: expecting EOF, found '}'.
Assets/Scripts/playertouchablefunction.js(1,23): BCE0043: Unexpected token: :.
Assets/Scripts/playertouchablefunction.js(4,12): UCE0001: ';' expected. Insert a semicolon at the end.
Assets/Scripts/playertouchablefunction.js(8,5): UCE0001: ';' expected. Insert a semicolon at the end.
Assets/Scripts/playertouchablefunction.js(9,12): UCE0001: ';' expected. Insert a semicolon at the end.
Assets/Scripts/playertouchablefunction.js(11,32): BCE0044: expecting ), found 'hit'.
Assets/Scripts/playertouchablefunction.js(11,35): BCE0044: expecting ), found ','.
Assets/Scripts/playertouchablefunction.js(11,37): BCE0043: Unexpected token: 10f.
Assets/Scripts/playertouchablefunction.js(14,2): BCE0043: Unexpected token: if.
Assets/Scripts/playertouchablefunction.js(14,48): UCE0001: ';' expected. Insert a semicolon at the end.
Assets/Scripts/playertouchablefunction.js(17,48): BCE0044: expecting :, found ';'.
Woops, sorry.. I'm still getting these errors though:
Assets/Scripts/papertouchable.cs(10,15): error CS1061: Type UnityEngine.GUITexture' does not contain a definition for
SetActiveRecursively' and no extension method SetActiveRecursively' of type
UnityEngine.GUITexture' could be found (are you missing a using directive or an assembly reference?)
Assets/Scripts/papertouchable.cs(15,15): error CS1061: Type UnityEngine.GUITexture' does not contain a definition for
SetActiveRecursively' and no extension method SetActiveRecursively' of type
UnityEngine.GUITexture' could be found (are you missing a using directive or an assembly reference?)
Sorry, it was pretty late when i posted that ;)
I had actually fixed that line.. not quite sure how it reverted.. but anyway, in TouchableObject.cs:
change this:
public GUITexture displayImage;
to this:
public GameObject displayImage;
I went ahead and changed it in the original post above as well.
Answer by Radon · Aug 02, 2012 at 05:03 AM
As DNP said. Create a GUI Text and apply the myGUIText.enabled = false;
to it. Then create and make if(Input. GetButtonDown("Y")) then myGUIText.enabled = true;
. MyGuiText as in your gui text name.
Well, I have the papers that I want to appear already done in Photoshop (I decided to go with a piece of paper ins$$anonymous$$d of a book). Is there a way to have the player click on the paper and then have the paper appear on their screen so they can read it?
Answer by Loius · Aug 03, 2012 at 02:57 AM
For pausing, one method is to set Time.timeScale to 0.0001 while the game is "paused" and the animation is playing. Play the animation at 10,000 times normal speed (because 10000 * 0.0001 = normal speed). While there's nothing playing that relies on time, set timeScale to zero (so nothing moves if players pause for days at a time). Then when you unpause set timeScale back to 1.0.
Answer by DNP · Aug 02, 2012 at 04:31 AM
I personally havent played skyrim. But what you could do is make a GUITexture Appear once you click on the book, and the have it disappear when you click again.
Answer by Radon · Aug 03, 2012 at 02:52 AM
Create a cubes and drag each of your papers (photos into those cubes or change the render image). Resize your cube so it fits your cube cover. Make your cubes children of your main camera and set it in front of the camera. Set theme all deactivated when the scene starts so you can't see anything. Then apply each by the following code
var First: GameObject;
var Second: GameObject;
var Third : GameObject;
function Start ()
{
First.SetActiveRecursively(false);
Second.SetActiveRecursively(false);
Third.SetActiveRecursively(false);
}
function Update ()
{
if(Input.GetKeyDown("1")){
First.SetActiveRecursively(true);
Second.SetActiveRecursively(false);
Third.SetActiveRecursively(false);
}
if(Input.GetKeyDown("2" )){
First.SetActiveRecursively(false);
Second.SetActiveRecursively(true);
Third.SetActiveRecursively(false);
}
if(Input.GetKeyDown("3" )){
First.SetActiveRecursively(false);
Second.SetActiveRecursively(false);
Third.SetActiveRecursively(true);
}
}
Thank you so much! Is there a way I can use this so if I click on an object in the game, this appears? Ins$$anonymous$$d of pressing a button?
Please make a different topic and I will surely help you. I'm always glad to help unity users.
Your answer
Follow this Question
Related Questions
Player Animation change when pickup Weapon 0 Answers
Trail of color behind the player 2D c# 1 Answer
Idle Animations, Player Walking 0 Answers
Create Player From Values [ Photon ] 0 Answers
How to move player after animation 2 Answers