Draggable door help needed!

Hello,

First time poster here, so here it goes, the problem i am having is that i can grab items which float in front of player in FPS camera it works all fine i can go around with it i can throw items, the next thing is Doors, as i tried to have same mechanism for doors as items just that i use a hinge that is blocking player from taking the door and walking with it, so the doors are unbreakable. i am currently testing the script with the game so thats why i has some random key inputs but its inputed as it is written in the scripts.

The result i want is that when i press LMB the door pushes so it opens, when RMB is hold in the range of the use door / open door it drags near player so it can be closed as well, as of now it is only possible to open but cannot close, to close i must go behind the door and push it back… that is a big bug for the game as the doors arent supposed to be opening in so much angle.

the whole point with the door push and pull system is that the player has the power to open door slowly or fast as well as only a little and the whole way and all in between for more realistic experience.

the same will work with windows in the future and other hinged doors such as trapdoors, and other entrances.

kind a new to scripting have been learning C# Sharp for 2 weeks only now so kind of a newbie still :slight_smile:

Hope to get this fixed as it is important aspect of the game as the game has to do a lot with different entrances as it is a horror FPS horror game :slight_smile:

Also like said i havent scripted so much in my life yet so i know its possible but i am not able to cut back on the script and make it shorter :-/ any advice on that would be also helpful

/*
PlayerEnvironmentPhysics.cs version[03.05.16] ~ Description: ""Drag, Drop & Throw Objects & Draggable Door & PickupObjects""
*/

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

[System.Serializable]
public class GrabObjectClass
{
    public bool m_FreezeRotation;
    public float m_PickupRange = 3f;
    public float m_ThrowStrength = 50f;
    public float m_distance = 3f;
    public float m_maxDistanceGrab = 4f;
}

[System.Serializable]
public class ItemGrabClass
{
    public bool m_FreezeRotation;
    public float m_ItemPickupRange = 2f;
    public float m_ItemThrow = 45f;
    public float m_ItemDistance = 1f;
    public float m_ItemMaxGrab = 2.5f;
}

[System.Serializable]
public class DoorPushClass
{
    public bool m_FreezeRotation;
    public float m_DoorPushRange = 2f;
    public float m_DoorPushThrow = 10f;
    public float m_DoorPushDistance = 3f;
    public float m_DoorPushMaxGrab = 3f;

}

[System.Serializable]
public class DoorPullClass
{
    public bool m_FreezeRotation;
    public float m_DoorPullRange = -2f;
    public float m_DoorPullThrow = -10f;
    public float m_DoorPullDistance = -2f;
    public float m_DoorPullMaxGrab = -3f;

}

[System.Serializable]
public class TagsClass
{
    public string m_HoldItemTag = "HoldItem";
    public string m_InteractItemsTag = "InteractItem";
    public string m_DoorsTag = "Door";
}

public class DragRigidbodyUse : MonoBehaviour
{

    public GameObject playerCam;

    public float ItemHoldX = 0.5f;
    public float ItemHoldY = 0.5f;

    public string PushButton = "Push";
    public string PullButton = "Pull";
    public string GrabButton = "Grab";
    public string ThrowButton = "Throw";
    public string ThrowButtonDoor = "ThrowDoor";
    public string UseButton = "Use";
    public GrabObjectClass ObjectGrab = new GrabObjectClass();
    public ItemGrabClass ItemGrab = new ItemGrabClass();
    public DoorPushClass DoorPush = new DoorPushClass();
    public DoorPullClass DoorPull = new DoorPullClass();
    public TagsClass Tags = new TagsClass();


    private float PickupRange = 3f;
    public float PickupSpeed = 10f;
    private float ThrowStrength = 50f;
    private float distance = 3f;
    private float maxDistanceGrab = 4f;

    private Ray playerAim;
    private GameObject objectHeld;
    private GameObject doorHeld;
    private bool isObjectHeld;
    private bool isDoorHeld;
    private bool tryPickupObject;
    private bool tryOpenDoor;


    void Start()
    {
        isDoorHeld = false;
        isObjectHeld = false;
        tryPickupObject = false;
        tryOpenDoor = false;
        objectHeld = null;
    }

    void FixedUpdate()
    {
        if (Input.GetButton(GrabButton))
        {
            if (!isObjectHeld)
            {
                tryPickObject();
                tryPickupObject = true;
            }
            else {
                holdObject();
            }
        }
        else if (isObjectHeld)
        {
            DropObject();
        }

        /////////////////////////////////////////////////////////

        if (Input.GetButton(PushButton))
        {
            if (!isDoorHeld)
            {
                tryToOpenDoor();
                tryOpenDoor = true;
            }
            else {
                holdDoor();
            }
        }
        else if (isDoorHeld)
        {
            DropObject();
        }

        /////////////////////////////////////////////////////////

        if (Input.GetButton(ThrowButton) && isObjectHeld)
        {
            isObjectHeld = false;
            objectHeld.GetComponent<Rigidbody>().useGravity = true;
            ThrowObject();
        }

        ///////////

        if (Input.GetButton(ThrowButtonDoor) && isDoorHeld)
        {
            isDoorHeld = false;
            doorHeld.GetComponent<Rigidbody>().useGravity = true;
            ThrowDoor();
        }
       
        ////

        if (Input.GetButton(UseButton))
        {
            isObjectHeld = false;
            tryPickObject();
            tryPickupObject = false;
            Use();
        }
    }


    /// <summary>
    /// THIS IS "pickup object" script !
    /// </summary>
    ///

    private void tryPickObject()
    {
        Ray playerAim = playerCam.GetComponent<Camera>().ViewportPointToRay(new Vector3(0.5f, 0.5f, 0f));
        RaycastHit hit;

        if (Physics.Raycast(playerAim, out hit, PickupRange))
        {
            objectHeld = hit.collider.gameObject;
            if (hit.collider.tag == Tags.m_HoldItemTag && tryPickupObject)
            {
                isObjectHeld = true;
                objectHeld.GetComponent<Rigidbody>().useGravity = false;
                if (ObjectGrab.m_FreezeRotation)
                {
                    objectHeld.GetComponent<Rigidbody>().freezeRotation = true;
                }
                if (ObjectGrab.m_FreezeRotation == false)
                {
                    objectHeld.GetComponent<Rigidbody>().freezeRotation = false;
                }
                /**/
                PickupRange = ObjectGrab.m_PickupRange;
                ThrowStrength = ObjectGrab.m_ThrowStrength;
                distance = ObjectGrab.m_distance;
                maxDistanceGrab = ObjectGrab.m_maxDistanceGrab;
            }
            if (hit.collider.tag == Tags.m_InteractItemsTag && tryPickupObject)
            {
                isObjectHeld = true;
                objectHeld.GetComponent<Rigidbody>().useGravity = true;
                if (ItemGrab.m_FreezeRotation)
                {
                    objectHeld.GetComponent<Rigidbody>().freezeRotation = true;
                }
                if (ItemGrab.m_FreezeRotation == false)
                {
                    objectHeld.GetComponent<Rigidbody>().freezeRotation = false;
                }
                /**/
                PickupRange = ItemGrab.m_ItemPickupRange;
                ThrowStrength = ItemGrab.m_ItemThrow;
                distance = ItemGrab.m_ItemDistance;
                maxDistanceGrab = ItemGrab.m_ItemMaxGrab;
            }
        }
    }

    private void holdObject()
    {
        Ray playerAim = playerCam.GetComponent<Camera>().ViewportPointToRay(new Vector3(ItemHoldX, ItemHoldY, 0f));

        Vector3 nextPos = playerCam.transform.position + playerAim.direction * distance;
        Vector3 currPos = objectHeld.transform.position;

        objectHeld.GetComponent<Rigidbody>().velocity = (nextPos - currPos) * PickupSpeed;

        if (Vector3.Distance(objectHeld.transform.position, playerCam.transform.position) > maxDistanceGrab)
        {
            DropObject();
        }
    }
   
    private void DropObject()
    {
        isObjectHeld = false;
        tryPickupObject = false;
        objectHeld.GetComponent<Rigidbody>().useGravity = true;
        objectHeld.GetComponent<Rigidbody>().freezeRotation = false;
        objectHeld = null;
    }

    private void ThrowObject()
    {
        objectHeld.GetComponent<Rigidbody>().AddForce(playerCam.transform.forward * ThrowStrength);
        objectHeld.GetComponent<Rigidbody>().freezeRotation = false;
        objectHeld = null;
    }

    private void Use()
    {
        objectHeld.SendMessage("UseObject", SendMessageOptions.DontRequireReceiver); //Every script attached to the PickupObject that has a UseObject function will be called.
        objectHeld = null;
    }


    /// <summary>
    /// THIS IS "push door" script !
    /// </summary>
    ///

    private void tryToOpenDoor()
    {
        Ray playerAim = playerCam.GetComponent<Camera>().ViewportPointToRay(new Vector3(0.5f, 0.5f, 0f));
        RaycastHit hit;

        if (Physics.Raycast(playerAim, out hit, PickupRange))
        {
            doorHeld = hit.collider.gameObject;
            if (hit.collider.tag == Tags.m_DoorsTag && tryOpenDoor)
            {
                isDoorHeld = true;
                doorHeld.GetComponent<Rigidbody>().useGravity = false;
                if (DoorPush.m_FreezeRotation)
                {
                    doorHeld.GetComponent<Rigidbody>().freezeRotation = true;
                }
                if (DoorPush.m_FreezeRotation == false)
                {
                    doorHeld.GetComponent<Rigidbody>().freezeRotation = false;
                }
                /**/
                PickupRange = DoorPush.m_DoorPushRange;
                ThrowStrength = DoorPush.m_DoorPushThrow;
                distance = DoorPush.m_DoorPushDistance;
                maxDistanceGrab = DoorPush.m_DoorPushDistance;
            }
        }
    }

    private void holdDoor()
    {
        Ray playerAim = playerCam.GetComponent<Camera>().ViewportPointToRay(new Vector3(0.5f, 0.5f, 0f));

        Vector3 nextPos = playerCam.transform.position + playerAim.direction * distance;
        Vector3 currPos = doorHeld.transform.position;

        doorHeld.GetComponent<Rigidbody>().velocity = (nextPos - currPos) * 10;

        if (Vector3.Distance(doorHeld.transform.position, playerCam.transform.position) > maxDistanceGrab)
        {
            dropDoor();
        }
    }

    private void dropDoor()
    {
        isDoorHeld = false;
        tryOpenDoor = false;
        doorHeld.GetComponent<Rigidbody>().useGravity = true;
        doorHeld.GetComponent<Rigidbody>().freezeRotation = false;
        doorHeld = null;
    }

    private void ThrowDoor()
    {
        doorHeld.GetComponent<Rigidbody>().AddForce(playerCam.transform.forward * ThrowStrength);
        doorHeld.GetComponent<Rigidbody>().freezeRotation = false;
        doorHeld = null;
    }

    private void Open()
    {
        doorHeld.SendMessage("OpenDoor", SendMessageOptions.DontRequireReceiver); //Every script attached to the Door that has a OpenDoor function will be called.
        doorHeld = null;
    }
}

The doors shouldn’ t have the same mechanics as ‘pick up’ objects except with a hinge. All it should be is animation that toggles open or closed depending on where the player is, using physic overlap sphere is one way to do it.

Yes it can be used as the pickup seen it done before and it worked for people long time now, my door as well is working to open the only problem is closing is as i need the door to move towards players on RMB hold, cause on LMB hold it pushed forward from the players center (pivot)

Also i do not want the typical press F / (Use button) to open/close entrances i want the mouse to be controlling the entrances once the right button is being held so it has that feeling as the players has a hold of the entrance as the player can hold, take, throw items in the environment.

anybody ?

It’s possible people are not understanding your question / problem.

The hinge has constraints on the min and max angles, it has a motor to control opening and closing, and a damper. Are you using those, or having trouble using those?

Hinge joints were bugged about a year ago, but I think they got them fixed. Could be not everything is fixed.