For a while now I have been working on a game of mine and from the beginning I have had problems with the rays and raycasting. The mouse is centered in the screen. At first I didn’t know what was happening, but then I made an upgrade menu which uses real gameobjects as buttons, where the mouse isn’t locked in the center of the screen and can be seen, and I noticed a big offset from the ray collision with the button. I used the debug command in my script to draw the ray and it really was offset quite a bit even tho in the game my mouse was on the button. I fixed half the problem by telling the ray to be drawn on the center of the screen when the mouse is centered and it worked without problems, but in the upgrade menu the problem is still active. Here is my code:
if (Cursor.lockState == CursorLockMode.None) {
mousePos = Input.mousePosition;
} else { mousePos = new Vector2 (Screen.width * 0.5f, Screen.height * 0.5f); }
ray = Camera.main.ScreenPointToRay(mousePos);
Debug.DrawRay (ray.origin, ray.direction, Color.green);
if (Physics.Raycast (ray, out hit, rayDistance)) { … }
Are the upgrade menu objects rendered by the same camera as the rest of the scene? If not, you’ll want to use the upgrade menu camera to construct the ray if you’re intending to intersect with objects in the menu. You might also consider using layer masks with your raycast call both to be more efficient and to not get bogus intersections of things drawn by one camera but being intersected by a ray from a different camera.
Well there could be a lot going in with your Raycast:
- Raycasts only show up in the scene view unless you turn on Gizmos. In the upper right of the Game View is Maximize On play, Mute Audio, Stats, Gizmos. Make sure this is pressed in
- Rays are normalized to length 1. So the ray your casting there is of length 1 You might want to do this: ray.direction*100f
- The default duration is 0. So it draws it then it disappears. If your running this code in Update as you move the mouse you might see a lag before the next ray is drawn and it appears to be off
- Rays parallel to the camera’s view point will never show up in the Game View
Create This script but don’t attach to a game object:
public static class Utility {
public static void DrawDebugPoint(Vector3 pt, float size, Color color, float duration = Mathf.Infinity)
{
Debug.DrawLine(pt - Vector3.up * size, pt + Vector3.up * size, color, duration);
Debug.DrawLine(pt - Vector3.right * size, pt + Vector3.right * size, color, duration);
Debug.DrawLine(pt - Vector3.forward * size, pt + Vector3.forward * size, color, duration);
}
}
Then add this code to some script you have or just create a new script an attach it to a gameObject:
using UnityEngine;
public class GenericTest : MonoBehaviour
{
bool drawRaySpot = true;
void Update()
{
if (Input.GetMouseButtonUp(0))
{
Vector3 position = Input.mousePosition;
position.z = 20f;
Vector3 worldPosition = Camera.main.ScreenToWorldPoint(position);
Utility.DrawDebugPoint(worldPosition, 1f, Color.green,4f);
if (drawRaySpot)
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Vector3 rayWorldPosition = ray.origin + ray.direction * 20f;
Utility.DrawDebugPoint(rayWorldPosition, 1f, Color.red, 2f);
}
}
}
}
If you have gizmos turned on you should see the lines drawn directly where you put your mouse They should be red, then after 2 seconds turn green. This is because first we are directly finding the worldPosition 20 units away from the camera at the mouse point and coloring it green. Then we are using Raycasting to find the same point, and drawing some lines there in red. You can’t see the green lines to start with because the raycasting find the exact some point. But I only set them to draw for 2 seconds, and the green ones for 4.
So raycasting is working perfectly there must be something else going on with your code that is making it be offset.
Possibly if your looking at it in the Scene view it seems offset when its really not. Since in the scene view the rays are showing you the perspective lines that collapse to a point on your screen.