Stubborn Pick Up Items Script

I got everything under the hood to work like the raycast but the actual pick up action doesn’t happen. I tried to take the RayCast and the SphereCast if statements away but it didn’t help.
Help is appreciated.

My pick up script:

    using UnityEngine;
    using System.Collections;
    using CameraRayCastLine;
  
    namespace CarryingWithoutDrop {
  
    public class PickUp : MonoBehaviour {
      
        // The variables for the positions.
            // Offset to the character.
                public float OffsetPositionX = 1;
                public float OffsetPositionY = 1;
                public float OffsetPositionZ = 1;
            // Relative position to the character.
                float CharacterPositionX;
                float CharacterPositionY;
                float CharacterPositionZ;
            // The Spherecast information.
                // Relative position to the object for the SphereCast.
                    float ObjectPositionX;
                    float ObjectPositionY;
                    float ObjectPositionZ;
                // The radius of the object SphereCast.
                    public float SphereCastRadius = 2.5f;
              
        // The boolean that checks if the camera's RayCast is hit.
            public bool CameraRayCast;
          
        // The Player and the tagged objects.
            private GameObject CarryableObject;
            public GameObject Character;
          
        public void Start() {
            // The code that tells you how to pick up an object through the debug log.
                Debug.Log("Press E to pick up an object.");
            // The origin of the objects.
                CarryableObject = GameObject.FindGameObjectWithTag("Carryable");
                Character = GameObject.Find("Character");
            // The code for determining the position of the character.
                CharacterPositionX = Character.transform.position.x + OffsetPositionX;
                CharacterPositionY = Character.transform.position.y + OffsetPositionY;
                CharacterPositionZ = Character.transform.position.z + OffsetPositionZ;
            // The code for determining the position of the object for the SphereCast.
                ObjectPositionX = CarryableObject.transform.position.x;
                ObjectPositionY = CarryableObject.transform.position.y;
                ObjectPositionZ = CarryableObject.transform.position.z;
            // The code that references the raycast boolean of the camera.
                CameraRayCast = GameObject.Find("MainCamera").GetComponent<PickUpRayCast>().CameraRayCast;
      }
      
        void PickUpCode() {
            // A RayCast variable.
                RaycastHit hit;
            // If you're in the SphereCast.
                if(Physics.SphereCast(new Vector3 (ObjectPositionX, ObjectPositionY, ObjectPositionZ), SphereCastRadius, transform.forward, out hit, 10)) {
                    // If you're looking at the box collider trigger.
                        if (CameraRayCast == true) {
                            Debug.Log("Press E to pick up the" + CarryableObject.name + ".");
                            // If you press the E key.
                                if (Input.GetKeyDown("e")) {
                                    // The position relative to the character.
                                        CarryableObject.transform.position = new Vector3(CharacterPositionX, CharacterPositionY, CharacterPositionZ);
                                    // The rotation relative to the character.
                                        CarryableObject.transform.rotation = Character.transform.rotation;
                                    // The gravity.
                                        CarryableObject.GetComponent<Rigidbody>().useGravity = false;
                                    // A message showing that the object was picked up.
                                        Debug.Log("You picked up the " + CarryableObject.name + "! Good for you!");
                                        Debug.Log("Press the left mouse button to use an object.");
                                        Debug.Log("Press E to drop an object.");  
                        }
                    }
                }
            }
        }
    }

That seems slightly strange for code. You want to be near an object and if it’s a pickup type, you can pick it up with the ‘e’ key…
Well, what will it be when it’s picked up? Will you SetParent to the pickup item so you carry it around with you? It looks kinda like that’s what was supposed to happen, but you never actually do that.
It’s also a bit odd that you don’t check what you hit but I guess if your logs print out correctly, it’s sort of working…

Are you wanting it to be a child object when you’re in range and have hit ‘e’ ?

The function PickUpCode() is never called. You presumably want that in Update().

lol wow…

Some other notes:
A) Use Vector3 instead of making 3 separate float variables. It’s super convenient.

public Vector3 offsetPosition = new Vector3(1,1,1);

You can then use offsetPosition directly in functions, do math on it, etc.
B) You do a lot of stuff in Start that should probably be in Update(). This code as written will get the player’s starting position, and then never update it again.
C) Don’t use GameObject.Find. This use case is begging for a singleton.
D) This code appears to be written intending to be run on neither the player nor the pickup object?

1 Like

One of my initial instincts was to suggest using a sphere collider setup around any object you want to be able to pick up that will show a (either) UI Message about being able to use ‘e’ or a UI Button (which could be clicked or activated with a hotkey like ‘e’) that would stay visible until you exit the trigger area.

Thank you for the help. I now changed the script and added in the setParent. And by the way, I have a layermask on my camera that checks what is hit. It’s in another script but I’ve referenced it.

Thank you for helping me. by the way, the script is supposed to be a scriptableobject script.

Whatever works… hopefully it’s all good for now… Good luck with your project and you’re welcome.