Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by TheDevGuy · Jan 12, 2016 at 11:13 PM · physicsraycastraycasthit

Physics.Raycast issue | Ray not casting at certain angles?

So i am creating a camera orbit script right now and want to detect if anything is between the player and the camera.

For some reason, the ray is only getting cast when the camera is in front of the player and not behind him at an angle.

Here is my code along with some screenshots to the issue:

IN FRONT OF THE PLAYER alt text


BEHIND THE PLAYER alt text

You can see that the debug line is not even being drawn at that angle for some reason.

 RaycastHit hit;
 Ray ray = new Ray(RaycastOrigin.transform.position, RaycastTarget.transform.position);
        
 if (Physics.Raycast(ray, out hit))
 {
 RaycastObstacle = hit.transform.gameObject;
 Debug.DrawLine(RaycastOrigin.transform.position, RaycastTarget.transform.position, Color.red);
 value = RaycastObstacle;
 }


2.png (293.8 kB)
1.png (293.9 kB)
Comment
Add comment
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

1 Reply

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

Answer by JedBeryll · Jan 12, 2016 at 11:30 PM

Common mistake: a raycast takes a starting point and a direction not an ending point. Either use LineCast which takes ending point instead, or calculate direction:

 Vectror3 rayDirection = RaycastTarget.transform.position - RaycastOrigin.transform.position;
 Ray ray = new Ray(RaycastOrigin.transform.position, rayDirection);
Comment
Add comment · Show 3 · 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 TheDevGuy · Jan 13, 2016 at 12:07 AM 0
Share

@JedBeryll

I looked into linecast but didnt know if it was the same as raycast since raycast can get "hit.transform.gameobject", which is what i need.

How would i get that infor using linecast? Or how would i be able to just get a "direction" that can point to my target using raycast?

avatar image JedBeryll TheDevGuy · Jan 13, 2016 at 07:03 AM 0
Share

"Or how would i be able to just get a "direction" that can point to my target using raycast?" I did just write that:

 Vectror3 rayDirection = RaycastTarget.transform.position - RaycastOrigin.transform.position;
  Ray ray = new Ray(RaycastOrigin.transform.position, rayDirection);

"How would i get that infor using linecast?" : The same way. public static bool Linecast(Vector3 start, Vector3 end, out RaycastHit hitInfo, int layer$$anonymous$$ask = DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal);

 RaycastHit hit;
 Physics.Linecast(RaycastOrigin.transform.position, RaycastTarget.transform.position, out hit);
avatar image TheDevGuy JedBeryll · Jan 13, 2016 at 10:48 AM 0
Share

@JedBeryll

Thank you for your response! Helped me learn more about raycasts and linecasts.

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

44 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Raycasting will not hit some imported meshes with mesh colliders? 2 Answers

RaycastHit if/else Problem (bug?) 0 Answers

Raycast problem 1 Answer

What is wrong about this RaycastHit Event??? 1 Answer

Having a RaycastHit event create a component? 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