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?
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.
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..
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)
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.
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.
– DracoratBunny83's code below was what I was looking for.. But thanks for the suggestions anyway! :)
– threedollarbill