- Home /
Trying to cast a ray towards an imaginary wall and get a "hit" / "end" point
Hello,
I am aiming my camera and casting a ray from it. Normally, you can get the "end" point by aiming at a target / obstacle like this:
Ray rayCast = new Ray(theCamera.transform.position, theCamera.transform.forward);
float dist = 100f;
RaycastHit hit;
// if this ray hits an obstacle, we can get the "end" point and it's distance
if(Physics.Raycast(rayCast, out hit, dist))
{
// the "end" point or "hit" point can be accessed via hit.point
// we can now illustrate the distance between start and end point
Debug.DrawLine(rayCast.origin, hit.point);
}
But what if there is no obstacle and I wanted to get an "end" point in mid-air (as if there was an imaginary wall right in front of me)? For example, what if the origin point of my ray has a Z (forward axis) of 3, and then I wanted to keep the ray going until it's Z (forward axis) is at 70? How would I go about coding that?
First of all you have to be more precise. "in front" can mean a lot. Should the "wall" be at a constant position in your scene? or should it always be "in front" of your player (which would mean it moves along when you rotate). Next thing is what kind of game do you actualy have and what's the players ability of movement (rotate / position change)?
You should make yourself clear where the wall should be positioned in space / relative to the player. If you can't describe it in words you may want to draw a scribble for us?
I guess the best thing would be to use a Plane, but your first problem is that you have to decide where it should be positioned.
"in front" doesn't say anything about the orientation of the "wall". If you consider a mathematical plane, can you tell us what the normal of the plane should look like?
edit
Feel free to edit your question
Ok. This is sort of like an archer / shooting game. The camera is just stationary, facing the Z direction, and it is able to rotate up/down, left/right to a certain extent (probably limited up to around 60 degrees).
Let's just imagine that I have a big wall in front of me, with targets (could be balloons or something) pinned to it. If I cast a ray and it hits a target, i should destroy and remove the target. If I cast a ray and it doesn't hit a target, I should represent what part of the wall was hit by pinning a small sphere / square / or something like that onto it (based on the XYZ of the hit point). As the game progresses, the wall (along with its pinned targets) gets farther and farther.
If the wall was just right in front of me, it would be easy to make it big enough so that no matter how much I rotate the camera, whether it's 60 degrees to the left/right or 60 degrees up/down, it would still be pointing at a direction where it's ai$$anonymous$$g at the wall.
However, as the wall gets farther and farther, you can just imagine how smaller it will become; and there will come a time when it would be possible that the camera can rotate and not be ai$$anonymous$$g at / looking at the wall anymore, and ins$$anonymous$$d just looking at mid-air. This is what I'm trying to avoid.
One solution could be to make the wall bigger as it gets farther, to make sure that no matter how much the camera rotates (limited to 60 degrees), it will still be ai$$anonymous$$g at the wall. But I was wondering if there is any smarter solution for this.
I hope I'm making sense as my brain is a little exhausted right now hahaha... If not, I'll just try to explain again next time when my $$anonymous$$d is clearer...
You aren't using gravity? Because if you are, you're approaching it completely wrong anyway. You should be projecting a physics object out and letting it fall where it will. If it then intersects a balloon or whatever - handle it. If it hits geometry, like your scene, leave it, replace with a stand-in - whatever and move on. Then you only need terrain to handle the possible furthest shot that accounts for gravity.
If you're not using gravity, scale the wall. Who cares if it's huge.
Bunny83's code below was what I was looking for.. But thanks for the suggestions anyway! :)
Answer by Bunny83 · Nov 13, 2013 at 05:21 PM
Ok, like i said above it's still not clear but i'll try:
float dist = 100f;
Plane backPlane = new Plane(new Vector3(0,0,-1), new Vector3(0,0,0));
void CastRay()
{
Ray ray = new Ray(theCamera.transform.position, theCamera.transform.forward);
RaycastHit hit;
if(Physics.Raycast(ray, out hit, dist))
{
// we hit an object at "hit.point"
}
else
{
float hitDist;
if (backPlane.Raycast(ray, out hitDist))
{
Vector3 point = ray.GetPoint(hitDist);
// we hit the back plane at "point"
}
}
}
The first argument of the Plane constructor is the plane's normal in worldspace. Keep in mind you can only hit the plane on the front side. If the plane faces the wrong way you can't hit it. The second argument is a worldspace position to define the location of the plane. My plane is located at the worlds origin and is facing the -z direction. With a usual setup (camera at 0,1,-10) you should look right at the plane.
Answer by Dracorat · Nov 13, 2013 at 04:27 PM
If the Raycast
fails to hit something, then you can just add the positions together to get the position at the end.
Vector3 newPosition = theCamera.transform.position + (theCamera.transform.forward * dist);
Not quite what I was trying to do. Sorry I'm having a hard time explaining this. I am trying to make it act as if there was an imaginary wall in front. How would I go about that without putting an actual physical collider right in front of me?
I converted your comment to a comment (it's not an answer). If that's what you want to do then you SHOULD put a collider there. You don't have to have a mesh renderer with a collider. Thus, it would actually be invisible and have no "drawing" cost.
Point taken.. But let's say the camera can rotate 60 degrees up/down and left/right.. and let's say the maximum Z distance I'm trying to limit my end point to is 500f (which means it's very far).. then I would need to create a really huge collider just to cover that whole area..
Or just parent a collider to the camera so its position follows the camera's orientation. The fact it's out at 500f shouldn't matter.
hmmm.. thanks but i dont think that would work.. i am trying to have an imaginary wall in front of me, so that when i cast a ray, it would stop at the wall (Z axis).. and as the imaginary wall gets farther (as it's Z increases), you can just imagine how big I would have to make that wall just to make sure that even if I rotate the camera 60 degrees up/down left/right, that there would still be a wall for the ray to hit... (hope im making sense)
Your answer
Follow this Question
Related Questions
Raycast stop at distance 3 Answers
RaycastHit.distance 1 Answer
Hit distance Change light range 1 Answer
How to detect if NavAgent has reached hit.point? (2) Distance doesn't work? 1 Answer
get the point directly above the raycast collision? 1 Answer