- Home /
Simple crafting system ; item pickup
Hello i have two scripts that are supposed to work in tandem One is called manageInv.JS its supposed to track whether or not you have collected a log or a plank and if you do have a log or plank you can make multiple things from those. I have another script that i just named Raycast.JS which is supposed to cast a raycast line at objects and distinguish their tags, and if they have either of the tags it will display a text saying "pickup" and let you pick them up by pressing "E". It also destroys the object then tells the manageInv script that you have collected them. The problem that I am encountering is that when I look at the two object that have those two tags (Log and Plank) it doesn't display the text and doesn't let me pick them up and doesn't tell the manageInv script that the player has collected the objects. Here is my code.
manageInv.JS
#pragma strict
var logCollected : boolean = false;
var plankCollected : boolean = false;
private var showPlankGUI : boolean = false;
private var showWallGUI : boolean = false;
var Plank : Transform;
var Wall : Transform;
var player : Transform;
var Axe : Transform;
function Update()
{
if(plankCollected == true)
{
showWallGUI = true;
}
if(plankCollected == true && Input.GetKey("l"))
{
MakeWall();
}
if(logCollected == true)
{
showPlankGUI = true;
}
if(logCollected == true && Input.GetKey("p"))
{
MakePlank();
}
if(logCollected == true && plankCollected == true && Input.GetKey("o"))
{
MakeAxe();
}
}
function onGUI()
{
if(showWallGUI == true)
{
GUI.Box(new Rect (200, 200, 200, 25), "Press P to make a wall segment");
}
if(showPlankGUI == true)
{
GUI.Box(new Rect (200, 200, 200, 25), "Press P to make planks");
}
}
function MakePlank()
{
Instantiate (Plank, player.transform.position, Quaternion.identity);
logCollected = false;
showPlankGUI = false;
}
function MakeWall()
{
Instantiate (Wall, player.transform.position, Quaternion.identity);
plankCollected = false;
showWallGUI = false;
}
function MakeAxe()
{
Instantiate (Axe, player.transform.position, Quaternion.identity);
plankCollected = false;
}
Raycast.JS
#pragma strict
var Log : GameObject;
var Plank : GameObject;
private var canHover : boolean = false;
function Update()
{
var fwd = transform.TransformDirection(Vector3.forward);
var hit : RaycastHit;
if(Physics.Raycast(transform.position, fwd, hit))
{
if(hit.distance <= 5.0 && hit.collider.gameObject.tag == "Log")
{
canHover = true;
if(Input.GetKeyDown("e"));
{
Destroy(Log);
var ManageScript : ManageInv = GameObject.Find("First Person Controller").GetComponent(ManageInv);
ManageScript.logCollected = true;
}
}
else
{
canHover = false;
}
if(hit.distance <= 5.0 && hit.collider.gameObject.tag == "Plank")
{
canHover = true;
if(Input.GetKeyDown("e"));
{
Destroy(Plank);
ManageScript.plankCollected = true;
}
}
else
{
canHover = false;
}
}
}
function OnGUI()
{
if(canHover == true)
{
GUI.Box(Rect(Screen.width / 2 - 10, Screen.height / 1 - 100, 150, 20), "Pick up");
}
}
Answer by Habitablaba · Oct 17, 2014 at 07:31 PM
Casting a ray straight forward from your character at a distance of 5 will only find things that are at least as tall as the character's position. If your character's transform is located at the center, you'll miss short things for example.
You'll also need the object to be directly in front of you, so if it is slightly to the left or right, you may miss it.
Check out sphere cast, instead, and see if that doesn't work better for you
Your answer