Trying to eliminate telescoping

I’m trying to stop a grenade from telescoping through surfaces. Right now it just keeps passing though surfaces.

I assume it’s because it’s moving after the collision check, but I don’t know what to do about it.

    private void FixedUpdate()
    {
        CheckTelescoping();
    }

    void CheckTelescoping()
    {
        Vector3 positionMark = transform.position;
        var layerMask1 = 1 << 0;
        var myLayerMask = layerMask1;
        RaycastHit hit;
        if (Physics.Linecast(positionMark, transform.position, out hit, myLayerMask))
        {
            Debug.DrawLine(positionMark, transform.position);
            transform.position = hit.point;
            myRigidbody.velocity = Vector3.zero;
        }
        positionMark = transform.position;
    }

positionMask is the same as transform.position at the time of the linecast, so you are casting a line to and from the same point, which obviously can’t intersect anything.

Perhaps if you make positionMark a class field, and the only assign to it at the end of your function?

    private void FixedUpdate()
    {
        CheckTelescoping();
    }
    Vector3 positionMark;
    void CheckTelescoping()
    {
        var layerMask1 = 1 << 0;
        var myLayerMask = layerMask1;
        RaycastHit hit;
        if (Physics.Linecast(positionMark, transform.position, out hit, myLayerMask))
        {
            Debug.DrawLine(positionMark, transform.position);
            transform.position = hit.point;
            myRigidbody.velocity = Vector3.zero;
        }
        positionMark = transform.position;
    }

Any reason why you aren’t just letting the physics engine do its thing?

1 Like

I’m trying this out as a cheaper alternative.

Using physics for a grenade is negligible performance wise. And it will respond natural to being throwed in any kind of arc, a short vid from our game I did to answer another question a while ago

1 Like