0
\$\begingroup\$

I'm trying to make an object follow my cursor using raycasting. I'm creating the raycast each frame and checking the player's distance from the hit and whether or not the hit landed on an object tagged 'surface'. The code works fine with only one of those conditions, e.g. if I just check for the surface tag or just check the range, but when checking for both the object will only move to a few certain (apparently random) positions on the surface.

        if (Physics.Raycast(ray, out hit))
        {
                if (hit.transform.tag == "Surface" && Vector3.Distance(player.transform.position, hit.point) < range)
                {
                    obj.transform.Find("holo").gameObject.SetActive(true);
                    obj.transform.position = new Vector3(hit.point.x, hit.point.y + 0.5f, hit.point.z);
                }
            }     

At first, I thought maybe the double condition was causing lag, but I have a powerful pc and I struggle to believe that two conditions in a frame are enough to create a noticeable loss in performance.

Video of the bug: https://youtu.be/mLug2ThX2hg , Full script just in case: https://pastebin.com/ruUXivsc

Any pointers would be greatly appreciated :)

\$\endgroup\$
1
\$\begingroup\$

I managed to fix this by placing the surfaces inside their own layer, then only casting to that layer. This removed the need for the tag condition.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .