Forum rules - please read before posting.

Prevent clipping of held Objects

edited May 2023 in Technical Q&A

Adventure Creator 1.77.1
Unity 2021.3.23f
URP-Pipeline
3D Project, with First Person Player Prefab.

I've tried stacking the Cameras by adding one as a child of the MainCamera. Setting it's render type to Overlay and giving the Object a specific Layer, that only gets rendered on this Child Camera.
(Followed this Tutorial, most are alike: How to Fix Weapon Clipping in Unity)

The MainCamera is the active one when playing, so i thought culling the Object there and rendering it on the child camera would work.
But if you set the Layer of the Object to anything other than the default layer, the player can't interact with it anymore, and the Object "vanishes" and doesn't get rendered.

What would be the best way to prevent clipping of objects, when the Player is holding something?

Best regards

Comments

  • Sounds like you're on the right track with camera culling. To fix the vanishing issue, you can use a script that dynamically alters the layer at runtime - only placing it on your separate layer while the object is held, and back onto the Default layer when dropped.

    To do this, you can make use of AC's OnGrabMoveable and OnDropMoveable custom events.

    Create a new C# file named DynamicDragLayer and try attaching this to the draggable object:

    using UnityEngine;
    using AC;
    
    public class DynamicDragLayer : MonoBehaviour
    {
    
        public string heldLayer = "Held";
        public string defaultLayer = "Default";
    
        private void OnEnable ()
        {
            EventManager.OnGrabMoveable += GrabMoveable;
            EventManager.OnDropMoveable += DropMoveable;
        }
    
        private void OnDisable ()
        {
            EventManager.OnGrabMoveable -= GrabMoveable;
            EventManager.OnDropMoveable -= DropMoveable;
        }
    
        private void GrabMoveable (DragBase dragBase)
        {
            if (dragBase.gameObject == gameObject)
            {
                GetComponentInChildren<Renderer> ().gameObject.layer = LayerMask.NameToLayer (heldLayer);
            }
        }
    
        private void DropMoveable (DragBase dragBase)
        {
            if (dragBase.gameObject == gameObject)
            {
                GetComponentInChildren<Renderer> ().gameObject.layer = LayerMask.NameToLayer (defaultLayer);
            }
        }
    
    }
    
  • edited May 2023

    Thanks Chris, i forgot to add that it also had a Hotspot on it, and when you disable a Hotspot, the Layer gets changed to "Ignore Raycast", but i think i've understood the code a bit better.

    I've just hooked into OnHotspotInteract and checked for a tag. I don't know if my solution is janky, but it does work.
    I only need to set it once and the object gets put somewhere and is not going to be interacted with again.

    private void OnEnable() { EventManager.OnHotspotInteract += OnHotspotInteract; }
    
    
     private void OnHotspotInteract(Hotspot hotspot, AC.Button button)
     {
          if (hotspot == GetComponent<Hotspot>() && gameObject.tag == "Object")
           {
                Debug.Log("Player interacted with: " + hotspot, hotspot);
                 gameObject.layer = LayerMask.NameToLayer(objectLayer);
          }
     }
    

    If anyone wants to replicate (no guarantees):

    • Add another Layer, i called it "ObjectLayer", this one only shows Objects that should not clip into things.
    • Add a Camera as a child of your MainCamera. Set it's layer to "Ignore Raycast" and Render Type as Overlay, Clipping Planes 0.01, Rendering deactivate post processing and render shadows, no Occlusion culling, Culling mask deactivate all except your "ObjectLayer".
    • Add it on your MainCamera on the Stack
    • Give your no clipping Object a Tag, mine is "Object"
    • Put the above code on your Object.

    If i want to place that object somewhere, by using a hotspot, i clear the parent of the object delete/teleport it and replace it with another one.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.