Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by threedollarbill · Nov 13, 2013 at 04:25 PM · raycastdistancehitpoint

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?

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Nov 13, 2013 at 05:09 PM 0
Share

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

avatar image threedollarbill · Nov 13, 2013 at 05:54 PM 0
Share

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...

avatar image Dracorat · Nov 13, 2013 at 05:57 PM 0
Share

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.

avatar image threedollarbill · Nov 13, 2013 at 06:15 PM 0
Share

Bunny83's code below was what I was looking for.. But thanks for the suggestions anyway! :)

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image threedollarbill · Nov 13, 2013 at 06:14 PM 0
Share

Hey this works! Thanks for this!

avatar image
0

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);
Comment
Add comment · Show 10 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image threedollarbill · Nov 13, 2013 at 04:31 PM 0
Share

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?

avatar image Dracorat · Nov 13, 2013 at 04:32 PM 0
Share

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.

avatar image threedollarbill · Nov 13, 2013 at 04:40 PM 0
Share

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..

avatar image Dracorat · Nov 13, 2013 at 04:41 PM 0
Share

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.

avatar image threedollarbill · Nov 13, 2013 at 04:55 PM 0
Share

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)

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges