#Interaction sometimes not working?

1 messages · Page 1 of 1 (latest)

rocky geode
#

so i got this interaction code and sometimes it doesn't really work for some reason...

public void itemInteraction() 
{
    Ray ray = new Ray(playerCamera.transform.position, playerCamera.transform.forward);


    if (Physics.Raycast(ray, out hit, 3f, itemInteractiableMask | interactiableMask))
    {

        int hitLayer = hit.collider.gameObject.layer;

        
        if (Input.GetKeyDown(KeyCode.E))
        {

            if ((interactiableMask.value & (1 << hitLayer)) != 0)
            {
                interactable = hit.collider.gameObject;

                interactable.SetActive(false);

            }

            else if ((itemInteractiableMask.value & (1 << hitLayer)) != 0) 
            {
                interactableRb = hit.collider.GetComponent<Rigidbody>();
                interactbleItem = hit.collider.gameObject;
                if (isHeld == false)
                {
                    Debug.Log("picked up");

                    isHeld = true;

                    itemInteractionObject.localPosition = new Vector3(0.25f, -0.5f, 2f);
                    interactbleItem.transform.SetParent(itemInteractionObject.transform);

                    interactableRb.useGravity = false;



                }
                else
                {
                    Debug.Log("dropped");

                    isHeld = false;

                    interactableRb.useGravity = true;

                    interactableRb.AddForce(velocity, ForceMode.Impulse);

                    interactbleItem.transform.SetParent(null);



                }
            }

            
        }
    }
}
#

Rest of code, couldnt fit it in one message:

public class InteractionScript : MonoBehaviour
{
    //layermasks
    public LayerMask interactiableMask;
    public LayerMask itemInteractiableMask;

    //gameobject
    public Transform itemInteractionObject;
    public GameObject interactbleItem;
    public GameObject interactable;

    //raycast
    RaycastHit hit;

    //camera
    public Camera playerCamera;

    //velocity stuff
    Vector3 velocity;
    Vector3 currentItemInteractionObjectPosition;
    Vector3 previousItemInteractionObjectPosition;

    //bools
    public bool isHeld = false;

    //rb
    public Rigidbody interactableRb;

    // Start is called once before the first execution of Update after the MonoBehaviour is created
    void Start()
    {
        previousItemInteractionObjectPosition = itemInteractionObject.position;

    }

    // Update is called once per frame
    void Update()
    {

        currentItemInteractionObjectPosition = new Vector3 (itemInteractionObject.position.x, itemInteractionObject.position.y, itemInteractionObject.position.z);

        velocity = (currentItemInteractionObjectPosition -previousItemInteractionObjectPosition)/Time.deltaTime;

        previousItemInteractionObjectPosition = currentItemInteractionObjectPosition;

           


        if (isHeld == true)
        {
            interactbleItem.transform.rotation = itemInteractionObject.rotation;
            interactbleItem.transform.position = itemInteractionObject.position;
        }

        itemInteraction();
    }
hallow beacon
#

also what part in the video is showing it not working?

rocky geode
hallow beacon
#

I know what they are supposed to do in the code

rocky geode
hallow beacon
#

Again

#

I'm not asking for an explanation of what they do

#

I'm asking for you to show me what it is set to in the inspector

#

Can you show a screenshot of the inspector for this script in the scene?

#

And also can you explain what part in the video is "not working"

rocky geode
#

The part that doesn’t work is like sometimes the E button doesn’t work for like releasing stuff

#

As demonstrated by my attempts to throw it

#

There’s also audio in the video

hallow beacon
#

I would just expect the code to say like "if I hit E and am currently holding an object, release the object"

#

the raycast should be irrelevant if you're already holding the thing

#

That's how I would fix the code.

rocky geode
#

True

#

Thanks I’ll keep that in mind tommorow❗

rocky geode
#

Thanks @hallow beacon, your suggestion really helped me fix the code and it works now :D