• 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 CFF · Oct 24, 2012 at 07:49 PM · raycastvector3.up

Raycasting weird?

if I use the following code for my raycast:

RaycastHit hit;

if (Physics.Raycast(transform.position,Vector3.forward, out hit)){ float distanceToGround = hit.distance; }

RaycastHit hit;

if (Physics.Raycast(transform.position,Vector3.up, out hit)){ float distanceToGround = hit.distance; }

The raycast will start at an corner of the terrain and end at the transform.position of the player. instead of starting at the players position and moving forward or moving up.

but when I use -vector3.forward,-vector3.up,vector3.right or -vector3.right it works perfectly fine.

So what am I doing wrong? or is it just not posible to raycast forward or up?

Comment
Add comment · Show 3
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 · Oct 24, 2012 at 08:42 PM 0
Share

Your question is a bit confusing. When you want to hit the "ground", doesn't it make sense to cast "downwards" (so "-up"). I'm not sure what you actually want to hit. I guess a screenshot or a simple drawing would help.

If you Raycast along Vector3.up you can be sure that the raycast will go upwards from the given starting point. How do you know that it's pointing the wrong way?

Feel free to edit your question.

avatar image Seth-Bergman · Oct 24, 2012 at 09:13 PM 1
Share

rather than Vector3.forward, you should probably be using Transform.forward

http://docs.unity3d.com/Documentation/ScriptReference/Transform-forward.html

avatar image CFF · Oct 24, 2012 at 09:34 PM 0
Share

I know it's pointing the wrong way because I use:

Debug.DrawLine (transform.position,hit.point);

to show me how where the raycast is and where it's poiting to.

I am using transform.forward now and it apears to still go back to the cornner of the terrain when it can't hit anything but works fine when it can hit something.

picture so you get an better idea of what I mean. http://i135.photobucket.com/albums/q123/chrisheng/raycast_zps6c9da72a.png

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · Oct 24, 2012 at 09:43 PM

I'm pretty sure i know what's your fault, but you've missed to include that little detail in your question. Fact is the raycast function returns a boolean whether it hits something or not. When it did not hit anything, It won't fill the RaycastHit struct since there is no hit. I guess you just use the point in your hit structure which is just invalid / zero.

Your Debug.DrawLine doesn't represent the raycast. It just draws a line between your starting point and the "hit point" which is invalid when Physics.Raycast returns false.

If you want to draw your raycast direction, use:

 Debug.DrawRay(transform.position, Vector3.forward*10000.0f);

You can still draw a line to the hit point, but only when there is a hit.

Comment
Add comment · Show 2 · 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 CFF · Oct 24, 2012 at 09:59 PM 0
Share

that makes sence. I was doing it wrong then;)

avatar image Bunny83 · Oct 24, 2012 at 10:03 PM 0
Share

@CFF: You've posted your two comments as answers to your own question. Answers should answer the question, for everything else, use comments ("add new comment").

I've converted your answers into comments.

Also don't forget to tick an answer when your question has been answered.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

11 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

Related Questions

How do I add a property to my main camera and access it elsewhere? 1 Answer

SphereCast doesn't work with a layerMask 1 Answer

[SOLVED] How to set an object's rotation parallel to a wall. 1 Answer

Rotate an object perpendicular to an objects normals? 1 Answer

Using raycast and collider to increase int 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