I need to add a cooldown for my script to make sure that I don’t instantly drop the object as soon as I pick it up. I think I’d be something like float TimeDown = 1S;.
This is the important part of my script:
void Update() {
// If another item isn't picked up at the moment.
if(ItemPickedUp == false) {
// If you're looking at the item.
if (Physics.Raycast(CameraMain.transform.position, CameraMain.transform.forward, RayCastLength, RayMask.value)) {
// If you press the E key.
if (Input.GetKeyDown("e")) {
// The CarryableObject becomes a child of the Character.
CarryableObject.transform.parent = Character.transform;
// The position relative to the character.
CarryableObject.transform.position = new Vector3(Character.transform.position.x + 1, Character.transform.position.y + 1, Character.transform.position.z + 1);
// The rotation relative to the character.
CarryableObject.transform.rotation = Character.transform.rotation;
// The gravity.
CarryableObject.GetComponent<Rigidbody>().useGravity = false;
// The boolean that activates the boolean that is needed for the dropping code.
ItemPickedUp = true;
// The child collider if-statements.
if(Child1 != null){
Child1.GetComponent<Collider>().enabled = false;
}
if(Child2 != null){
Child2.GetComponent<Collider>().enabled = false;
}
if(Child3 != null){
Child3.GetComponent<Collider>().enabled = false;
}
if(Child4 != null){
Child4.GetComponent<Collider>().enabled = false;
}
}
}
}
// If you're holding an item.
if(ItemPickedUp == true){
// If you press the E key.
if(Input.GetKeyDown("e")){
// The CarryableObject becomes a child of the Utility Props again.
CarryableObject.transform.parent = UtilityProps.transform;
// The drop position.
CarryableObject.transform.position = new Vector3(Character.transform.position.x, Character.transform.position.y, Character.transform.position.z + OffsetDropPosition);
// The rotation is no longer relative to the Character.
CarryableObject.transform.rotation = Character.transform.rotation;
// The gravity gets reenabled.
CarryableObject.GetComponent<Rigidbody>().useGravity = true;
// The pick up boolean.
ItemPickedUp = false;
// The child collider if-statements.
if(Child1 != null){
Child1.GetComponent<Collider>().enabled = true;
}
if(Child2 != null){
Child2.GetComponent<Collider>().enabled = true;
}
if(Child3 != null){
Child3.GetComponent<Collider>().enabled = true;
}
if(Child4 != null){
Child4.GetComponent<Collider>().enabled = true;
}
}
}
}