Raycast is offsetting object
I'm shooting a simple Raycast out of the main camera. A object will then be instantiated that will follow the ray. However the object is getting offset off of the ray, which I don't want. Any idea why this is happening?
This is the Ray:
Ray ray = Camera.main.ViewportPointToRay(new Vector2(0.5f, 0.5f)); RaycastHit hit;
And this is the object's position:
transform.position = hit.point;
Here is a gif: link text
If anyone knows the solution or has some knowledge about this offset-thing, I would really appreciate an answer.
Im not sure, but seems like your Gizmos drawline is the incorrect thing not your cube. If you see the game Window, the right window, you are making a ray to the center on this windows, and the position seems to be fine (I cant see with precision)
If you share all code you are using, we can help you better, because I think the problem is on other way of your code. In this case I need to $$anonymous$$now How are drawing your red line
Answer by unidad2pete · Oct 28, 2017 at 08:46 PM
I dont understand what do you mean when you say " but when I move with the object in my hand, it moves the object further away when I move with it"
but referencing to the red line, I supose your script is the town, I dont know, but I try to explain me better.
Debug.DrawLine(transform.position, transform.forward * range, Color.red, 0.01f);
If the script is on the Town, the line direction its based on the forward of the Town but here:
Camera.main.ViewportPointToRay(new Vector2(0.5f, 0.5f));
the ray starts on the center of the game window to the center of the game window, then you are using 2 diferent rays.
// this is the ray of side off the town
RaycastHit hit;
if(Physics.Linecast(transform.position, transform.forward, out hit))
{
yourCube.transform.position = hit.point;
}
// this is the ray of your camera
Ray ray = Camera.main.ViewportPointToRay(new Vector2(0.5f, 0.5f));
if (Physics.Raycast(ray, out hit, range))
{
yourCube.transform.position = hit.point;
}
// You can see the diference drawing both lines
Debug.DrawLine(transform.position, transform.forward * range, Color.red, 0.01f);
Debug.DrawRay(ray.origin, ray.direction, Color.green, 0.01f);
Try both and test the diference.
The part of the object moevent, if you mean when the grid is actived the movement is diferent, is because the grid is based on position with any distance, I mean, you have a grid with 4*4 , then position on first grid is (0,0,0) , grid 2 (2,0,0) grid 3 (0,0,2) grid 4 (2,0,2), then the object teleport with distance of 2. When you moves the object in your hand, the object can move with precise positions, (0,0,0) , (0.1f, 0, 0) , (0.2f, 0,0) etc..
Do you mean this?
And be careful with the example of my answer Im using the same Raycasthit for both rays, you should make 2 rays to see the diference on same moment.
RaycastHit hit1; // hit for town ray
RaycastHit hit2; //hit for your camera
Thank you. The Raycasting works perfectly now. The object is exactly in the middle of the hitpoint of the ray now, just like how I wanted it to be.
And I also got the grid system working, because I had accidentally commented out the wrong line. So that works now too.