Same Raycast Hitting 2 Different Points

So, I am trying to make a grid placement system for some objects. The player will look around, and wherever the player looks, the object will snap to the nearest integer value Vector3 (Like 0, 2, 1 and not 3.1, 2.5, 1.7). However, when my raycast is near the 0.5 range of a value, it tries to snap to both sides on the grid. So, I tried using Debug.Log to see where the raycast was hitting, and I found out that the cast is actually hitting on both sides at the same time. I don’t have to touch anything at all, but it will still hit in two different spots. This is what I have.

            //Set ray
            ray = Camera.main.ScreenPointToRay(new Vector2(Screen.width / 2, Screen.height / 2));

            //Cast the Ray
            if (Physics.Raycast(ray, out hit, maxPlaceDistance))
            {
                float newX = Mathf.Round(hit.point.x);
                float newY = Mathf.Round(hit.point.y);
                float newZ = Mathf.Round(hit.point.z);

                //Set the position of the tower to the new position
                objectToPlace.transform.position = new Vector3(newX, newY, newZ);

                //Log the hit.point of the ray
                Debug.Log(hit.point);
            }

I have also tried using Input.mouseposition instead of the Vector2, and that didn’t help. I can’t figure out what’s happening any help would be greatly appreciated.

Highly unlikely given that it’s returning a single Vector3 as the hit point… I suspect what you are seeing is one frame it’s rounding up, next frame it’s rounding down as it sits right on the cusp of the rounding boundary and floating point inaccuracy comes into play.

1 Like

No, it actually is hitting 2 separate points. Like I said, I used Debug,Log to print out the actual non-rounded hit.point values. It is actually hitting separate points. I was extremely skeptical at first too, but I am positive that it is not a rounding issue, as I have tried other placement conditions other than rounding the values, but to no avail. I am 99% sure that it is a raycasting issue. There are other posts about this problem, but none of those helped my case any.

can you post a screen shot of those debug.log outputs (and indicate what the code of the debug.log call is :slight_smile: )?

The console is there at the bottom. As you can see, there are two values that it printed out a lot. I didn’t move the mouse, or have any other kind of input, but it still chose between those two values a randomly. When I start to look at the black grid lines in the game, is when the problem starts occurring. Also, I edited the original post to include the Debug.Log.

ok, so I’m guessing you’re placing a default unity cube with the origin in the center middle. You’re also not filtering out said cube from the raycast, so the first hit is a “0.1” or some such value, the value is rounded and the cube placed. The next hit is going to be on the cube at “rounded value + 0.5” which immediately puts the round in jeopardy of floating point inaccuracy (0.5000001 vs 0.49999999 would snap up/down).

So first thing to do would be to add a layermask to the raycast so you aren’t hitting the thing you are placing.

No, I thought about that issue before I even built this system. I knew that if I was raycasting that I would collide with what ever I was trying to place. So, I removed ALL collider components from the prefab that I’m trying to place. It is not colliding with the object at all. I am positive about that. I have triple checked that possibility. And also, the glitch happens on left/right borders, not just closer/further borders like that issue would cause.

See if this helps - https://forum.unity3d.com/threads/snapping-objects-within-gameworld.455508/

I made a grid system to move track pieces around in a game I’m making for my son. It does a little more than just round the values.

That’s a very robust system for snapping to a grid, but it’s still not what I need. My problem is not getting it to snap to the grid. That part works, it’s just the Raycast that that I’m doing keeps returning different points, even when I’m not changing it at all. If I can get that part working correctly, then it would all work properly. That’s my issue. And, it’s not like I’ve never used raycasts before. I have used them extensively for many things, but I’ve never had this problem before. I am at a total loss for why it is behaving this way.

Maybe it’s making up random z values as you aren’t giving it one.

Try this

ray = Camera.main.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0));
        Debug.DrawRay(ray.origin, ray.direction * maxPlaceDistance, Color.yellow);
        //Cast the Ray
        if (Physics.Raycast(ray, out hit, maxPlaceDistance))
        {
            float newX = Mathf.Round(hit.point.x);
            float newY = Mathf.Round(hit.point.y);
            float newZ = Mathf.Round(hit.point.z);

            //Set the position of the tower to the new position
            objectToPlace.transform.position = new Vector3(newX, newY, newZ);

            //Log the hit.point of the ray
            Debug.Log(hit.point);
        }

Thank you for suggesting to draw out the ray. I actually can’t believe that I hadn’t already done that. But, what I found is odd. As you can see in the picture, the ray that is drawn is coming from what I believe is 0.5 units below (In world space) where it normally should come from. It seems to randomly choose to generate the ray from either the center of the screen, or from the odd position below the camera. I tried the vector3 thing, and that did nothing. I also tried using the camera object and forward vector itself to make the ray instead of using Screenpointtoray, and that didn’t help. So, now I what is happening, but still no idea how to fix it.

Okay guys I solved the problem! Due to some other issues earlier in the project, I had to make some unusual changes to my player. So basically my player is constantly having a force applied downward to him. So the Physics.raycast must be activating when the physics is calculated (I guess that’s why it’s in the physics class). So my character is falling, then casting the ray, then before the screen renders, is setting it’s position back to normal. That’s why I could never see anything wrong, because visually I was never moving, but the raycast was just happening in a different place because of physics. When I lock my player’s position, it’s completely fixed. Not a single glitch. Thanks for all your help guys. I really do appreciate all that you guys did.

Could you not just cast your ray in fixedupdate to resolve the physics issue?

I didn’t actually try that. That probably would have been a much simpler fix then what I did. I already fixed the issue another way by changing my player accordingly. So, honestly, I’m not sure if that would have worked or not. It definitely would have been worth a shot.