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 ROM · Jul 28, 2011 at 01:28 PM · raycastlightrayfire

Raycasting error?

Basically I have an object which sends 2 raycasts out downwards to detect if anything is hit, I'm using this code:

 if (Physics.Raycast (Vector3(transform.position.x + 0.3, transform.position.y, transform.position.z), -Vector3.forward, hit, ydist, layerMask) || Physics.Raycast (Vector3(transform.position.x - 0.3, transform.position.y, transform.position.z), -Vector3.forward, hit, ydist, layerMask)) {
 }

They're hitting objects but not in the correct direction, it's odd because I also have rays firing out upwards, left and right and they all seem to be working correctly. The image below shows what I mean, the object with the x on it is the object which the script is contained on and the red line denotes the direction that the raycast should be fired (using Debug.Draw). The box which is outlined in red is the box which is hit which clearly isn't right.

alt text

Any help or ideas would be GREATLY appreciated.

Edit Note : Even if I reduce both of the "0.3's" to 0 it still doesn't work so thats not the problem :(

Comment
Add comment · Show 5
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 Graham-Dunnett ♦♦ · Jul 28, 2011 at 01:31 PM 0
Share

Vector3.right??? Don't you want Vector3.down? Admittedly I know nothing about the coordinate system you are using.

avatar image ROM · Jul 28, 2011 at 01:36 PM 0
Share

Sorry, I posted the wrong section of code I was thinking about firing right so much ^^. Updated :)

avatar image aldonaletto · Jul 28, 2011 at 01:59 PM 0
Share

Now you're casting the ray backwards (-Vector3.forward). Is that what you consider the down side? It should be -Vector3.up in a regular coordinate system

avatar image ROM · Jul 28, 2011 at 02:22 PM 0
Share

It's viewed from a top down perspective so I want to cast along the z axis :)

avatar image aldonaletto · Jul 28, 2011 at 06:24 PM 0
Share

If the ground in your game is the XY plane, the Z axis should be Vector3.forward, no? Anyway, that box is apparently out of the down direction. Could the box and its collider be misaligned? Raycast checks the collider, not the mesh. And which collider the box have? Box or mesh collider? $$anonymous$$esh colliders can produce some unexpected results, maybe due to incorrectly generated bounds. Another possibility could be some object childed to the box intercepting the ray.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by PaulUsul · Jul 29, 2011 at 09:19 AM

It's because you are firing towards vector(0,-1,0) from your position, making skewed lines instead of straigh lines from your position. something like this(C#)..

 Vector3 pos1 = transform.position;
 pos1.x += 0.3;
 Vector3 dir1 = pos1 - Vector3.forward;
 
 Vector3 pos2 = transform.position;
 pos2.x -= 0.3;
 Vector3 dir2 = pos2 - Vector3.forward;
 
 if (Physics.Raycast (pos1 , dir1 , hit, ydist, layerMask) || 
     Physics.Raycast (pos2 , dir2 , hit, ydist, layerMask)) {
 }
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 PaulUsul · Aug 01, 2011 at 12:52 PM 0
Share

Did this approach work, by firing from the target towards it's axis and not world space coords..?

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

My raycast is not always in the forward direction. 1 Answer

Problem with raycasting when checking neighbouring objects? 0 Answers

What is the best way to navigate through a grid based level using Raycasting ? 2 Answers

How could I combine melee weapons and rays? 1 Answer

Someone can help with Ray/Collide? 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