- Home /
Bugs in a pick up script
I have a couple of bugs that I can't seem to fix:
When I try to pick up an item I pick up all of the items that my script is attached to (and that are supposed to be able to be picked up).
The gravity doesn't turn off when I try to pick up an item.
I can't pick up 1 of the items that this script is attached to (, I can pick up the other ones work though).
The colliders on the children of the items don't turn off when I try to pick up the item.
The PickUpItem() and DropItem() voids are called in Update() when you press the E key.
Any help is appreciated.
This is my script:
using UnityEngine;
using System.Collections;
namespace CarryingItems {
public class PickUp : MonoBehaviour {
public float RayCastLength = 2.15F;
public LayerMask RayMask;
public float OffsetDropPosition = 2;
private GameObject Handle;
private GameObject HandleExtra;
private GameObject BladeHolder;
private GameObject Blade;
private GameObject CameraMain;
public GameObject CarryableObject;
private GameObject Character;
private GameObject UtilityProps;
private bool ItemPickedUp;
void Awake() {
CameraMain = GameObject.Find("MainCamera");
Character = GameObject.Find("Character");
UtilityProps = GameObject.Find("UtilityProps");
Handle = this.gameObject.transform.GetChild(0).gameObject;
HandleExtra = this.gameObject.transform.GetChild(1).gameObject;
BladeHolder = this.gameObject.transform.GetChild(2).gameObject;
Blade = this.gameObject.transform.GetChild(3).gameObject;
}
void Start() {
ItemPickedUp = false;
}
void Update() {
if (Input.GetKeyDown("e")) {
ItemPickUp();
ItemDrop();
}
}
void ItemPickUp() {
if(ItemPickedUp == false) {
if (Physics.Raycast(CameraMain.transform.position, CameraMain.transform.forward, RayCastLength, RayMask.value)) {
CarryableObject.transform.parent = Character.transform;
CarryableObject.transform.position = new Vector3(Character.transform.position.x + 1, Character.transform.position.y + 1, Character.transform.position.z + 1);
CarryableObject.transform.rotation = Character.transform.rotation;
CarryableObject.GetComponent<Rigidbody>().useGravity = false;
ItemPickedUp = true;
if(Handle != null){
Handle.GetComponent<Collider>().enabled = false;
}
if(HandleExtra != null){
HandleExtra.GetComponent<Collider>().enabled = false;
}
if(BladeHolder != null){
BladeHolder.GetComponent<Collider>().enabled = false;
}
if(Blade != null){
Blade.GetComponent<Collider>().enabled = false;
}
}
}
}
void ItemDrop() {
if(ItemPickedUp == true){
CarryableObject.transform.parent = null;
CarryableObject.transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z + OffsetDropPosition);
CarryableObject.transform.rotation = Character.transform.rotation;
CarryableObject.GetComponent<Rigidbody>().useGravity = true;
ItemPickedUp = false;
if(Handle != null){
Handle.GetComponent<Collider>().enabled = true;
}
if(HandleExtra != null){
HandleExtra.GetComponent<Collider>().enabled = true;
}
if(BladeHolder != null){
BladeHolder.GetComponent<Collider>().enabled = true;
}
if(Blade != null){
Blade.GetComponent<Collider>().enabled = true;
}
}
}
}
When is ItemPickUp()
called and when is CarryableObject
assigned?
Any reason why CarryableObject
is not passed as a parameter?
You should update your question accordingly.
Something about this doesn't seem right.
Why does your pickup button not trigger a raycast, then each item be a seperate object?
There is way too much going on here for a simple Raycast pickup script.
Would you please be so kind as to elaborate a little bit more on how I do the seperate objects thing?
I don't know what exactly you are ai$$anonymous$$g for, but I feel like this is more along the lines of what you want.
using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
namespace myGame
{
public class Inventory : $$anonymous$$onoBehaviour
{
public GameObject CarryableObjectSlot;
public GameObject CarriedItem;
public $$anonymous$$eyCode Pickup$$anonymous$$ey;
public float PickupRange;
public float TossRange;
void Update()
{
if (Input.Get$$anonymous$$ey(Pickup$$anonymous$$ey))
{
if (!CarriedItem)
{
PickupItem();
}
else
{
DropItem();
}
}
}
public void PickupItem()
{
Ray ray = new Ray(Camera.current.transform.position, Camera.current.transform.forward);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, PickupRange)){
if (hit.collider.tag == "CarryableObject")
{
CarriedItem = hit.collider.gameObject;
CarriedItem.transform.position = CarryableObjectSlot.transform.position;
CarriedItem.transform.rotation = CarryableObjectSlot.transform.rotation;
CarriedItem.transform.SetParent(CarryableObjectSlot.transform);
CarriedItem.GetComponent<Rigidbody>().useGravity = false;
}
}
}
public void DropItem()
{
CarriedItem.transform.position = transform.forward * TossRange;
CarriedItem.transform.SetParent(null);
CarriedItem.GetComponent<Rigidbody>().useGravity = true;
CarriedItem = null;
}
}
}
Answer by yaezah · May 14, 2017 at 06:13 PM
I think you might want to specify CarryableObject as that specific gameobject you're trying to pick up, instead of assigning it to every item on the scene (that's why your'e picking up all the items at once).
Also have you tried changing from HandleExtra.GetComponent().enabled = false; to HandleExtra.collider.enabled = false;
Would you please be so kind as to elaborate a little bit more on how I do the first thing that you said?
if (Physics.Raycast(Camera$$anonymous$$ain.transform.position, Camera$$anonymous$$ain.transform.forward, RayCastLength, Ray$$anonymous$$ask.value)) {
CarryableObject = this.gameObject;
CarryableObject.transform.parent = Character.transform;
I would try that above, then when you drop it make it null
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
For Some reason, collider check has extra collider. 2 Answers
Make sure levels assets are loaded before switching? 1 Answer