Custom script messing with items created with item manager

matmalm

Member
I have a custom script which allows me to open a drawer, however when I put a pickup item inside the drawer I can't open it anymore. The items is small enough to fit inside the drawer. The problem is when the sphere collider (trigger) of the pickup item collides with the drawer collider. But I can't make it smaller otherwise the player with not be able to pick up the item.

drawer.png


My custom script is this one:

C#:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class drawer : MonoBehaviour
{
    [SerializeField] float ySensitivity = 300f;
    [SerializeField] float frontOpenPosLimit = 45;

    [SerializeField] GameObject frontDoorCollider;

    bool moveDoor = false;
    DoorCollision doorCollision = DoorCollision.NONE;


    // Use this for initialization
    void Start()
    {
        StartCoroutine(doorMover());
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Debug.Log("Mouse down");

            RaycastHit hitInfo = new RaycastHit();
            if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo))
            {
                if (hitInfo.collider.gameObject == frontDoorCollider)
                {
                    moveDoor = true;
                    Debug.Log("Front door hit");
                    doorCollision = DoorCollision.FRONT;
                }
                else
                {
                    doorCollision = DoorCollision.NONE;
                }
            }
        }

        if (Input.GetMouseButtonUp(0))
        {
            moveDoor = false;
            Debug.Log("Mouse up");
        }
    }

    IEnumerator doorMover()
    {
        bool stoppedBefore = false;
        float zPos = 0;

        while (true)
        {
            if (moveDoor)
            {
                stoppedBefore = false;
                Debug.Log("Moving Drawer");

                zPos += Input.GetAxis("Mouse Y") * ySensitivity * Time.deltaTime;


                //Check if this is front door or back
                if (doorCollision == DoorCollision.FRONT)
                {
                    Debug.Log("Pull Down(PULL TOWARDS)");
                    zPos = Mathf.Clamp(zPos, -frontOpenPosLimit, 0);
                    Debug.Log(zPos);
                    transform.localPosition = new Vector3(0, 0, -zPos);
                }
            }
            else
            {
                if (!stoppedBefore)
                {
                    stoppedBefore = true;
                    Debug.Log("Stopped Moving Drawer");
                }
            }

            yield return null;
        }

    }


    enum DoorCollision
    {
        NONE, FRONT
    }
}

Don't mind about the word "door". I just modified a door script to do the same action with the drawer.


This is my layer collision matrix if it helps:

layer matrix.png
 
But then the raycast will not detect colliders which I use to open it
I may have misunderstood your code or the problem. Looking at your code, you do a raycast and if this returns a match with the gameObject variable frontDoorCollider, you set the variable movedoor to true, which the coroutine uses to start moving the gameObject the script is attached to. There are some performance issues with how this is done, but it should work. If this is not working, then the raycast is probably not returning the correct gameObject. Try putting Debug.Log(hitInfo.collider.gameObject.name) after the raycast to see what it returns. If it is returning the pickup item, then instead change your raycast to a raycastall and iterate through the list to check if any of the hits is the frontDoorCollider.
 
Can't you just put the door and item colliders on separate layers, then add a layer mask to your Raycast call, so that it only detects the layer that the door is on?
 
Top