- Home /
Having to press E thrice...
I have this script to pick up and hold items and it works fine but I have to press E thrice to drop an object.
This is my script:
using UnityEngine;
using System.Collections;
#pragma warning disable 0414
namespace CarryingItems {
public class PickUp : MonoBehaviour {
// publics
// The parent.
private GameObject UtilityProps;
// Where to place the item.
private GameObject Hand;
// The object I'm carrying.
public static GameObject CarriedObject;
public LayerMask RayMask;
public bool NearObject;
// privates
private bool ItemPickedUp;
private float RayCastLength = 2.15f;
private float OffsetDropPosition = 2;
private RaycastHit FilterHit;
void Awake() {
UtilityProps = GameObject.Find("UtilityProps");
Hand = GameObject.Find("ItemHand");
}
void Update() {
NearObject = PickUpSphereCasts.NearObject;
if (Input.GetKeyDown(KeyCode.E)){
if (ItemPickedUp == true) {
if(NearObject == true){
ItemDrop();
}
}
else if (Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out FilterHit, RayCastLength, RayMask.value)){
ItemPickUp(FilterHit.collider.gameObject);
}
}
}
void ItemPickUp(GameObject objectToPickup) {
// unless you've set your LayerMask to only hit the objects that can be picked up (you should do this as well, for optimization purposes), you must check if you can pick up this object
if (objectToPickup.tag != "Carryable") { // if you can't pick it up, do nothing then leave this function
return;
}
// from now on we know we are holding an item
ItemPickedUp = true;
// make the scene modifications
CarriedObject = objectToPickup;
CarriedObject.transform.SetParent(Hand.transform, false);
CarriedObject.GetComponent<Rigidbody>().useGravity = false;
CarriedObject.GetComponent<Rigidbody>().isKinematic = true;
CarriedObject.transform.position = Hand.transform.position;
CarriedObject.transform.rotation = Hand.transform.rotation;
CarriedObject.gameObject.GetComponent<Collider>().enabled = false;
}
void ItemDrop() {
// from now on, we do not have an object picked up
ItemPickedUp = false;
// make the scene modifications
CarriedObject.transform.SetParent(UtilityProps.transform, false);
CarriedObject.transform.position = Camera.main.transform.position + Camera.main.transform.forward * OffsetDropPosition;
CarriedObject.transform.rotation = Hand.transform.rotation;
CarriedObject.GetComponent<Rigidbody>().useGravity = true;
CarriedObject.GetComponent<Rigidbody>().isKinematic = false;
CarriedObject.gameObject.GetComponent<Collider>().enabled = true;
// clear the reference
CarriedObject = null;
}
}
}
Any help is appreciated.
What is ' PickUpSphereCasts.NearObject' ?
It looks as if you have to be close to an object before you're allowed to drop an object. Without seeing more of this code and understanding why it's there, the question is hard to answer.
That's a spherecast form another script that was placed in the wrong place. Thanks for the help.
Also, should be pretty straight forward to at least debug which of the ifs is preventing you from getting to that DropItem() line. Put some Debug.Log in both ifs and see which one is notletting you in.
Your answer
Follow this Question
Related Questions
Bugs in a pick up items script. 2 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
TextMeshPro Object not updating in real time 0 Answers
Stretched Particles Billboard 0 Answers