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
2
Question by DylanST · Oct 10, 2016 at 09:20 PM · c#collisionraycasttransform.position

Why is the ray cast pointing towards the origin of the scene?

I want the raycast to be pointing forward along the x axis of a cube in my scene. Instead the raycast starts at the origin of the cube and then points towards the origin of the scene (0,0,0). The length of the raycast is as long as it needs to be in order to reach the origin, despite me giving it a specific length.

  void Update () {
     RaycastHit hit;
     Vector3 fwd = transform.TransformDirection(Vector3.forward);
     Debug.DrawLine (transform.position, transform.forward, Color.red);

     if (Physics.Raycast (transform.position, fwd, out hit, 10)) { 
         print ("There is something in front of the object!");
     }
 }
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

4 Replies

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

Answer by Bunny83 · Oct 11, 2016 at 12:25 AM

Your problem is that you use Debug.DrawLine which expects two worldspace points. However you pass one worldspace point and a direction vector. Direction vectors are normalized (have a length of 1f). So since you treat the direction vector as position it's always around the origin (in the range +-1 on each axis).

Either use Debug.DrawRay like this:

 Debug.DrawRay(transform.position, transform.forward*10f, Color.red);

where "10" is the length of your ray, or use DrawLine like this:

 Debug.DrawLine (transform.position, transform.position + transform.forward*10f, Color.red);

which does the same thing. Your actual raycast was correct but your visualization wasn't.

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 DylanST · Oct 11, 2016 at 12:57 AM 1
Share

I feel kind of silly knowing that the problem had to do with the debug statement, but this fixed the problem. thanks for the help.

avatar image
0

Answer by tanoshimi · Oct 10, 2016 at 09:25 PM

"forward along the x axis"? Do you mean pointing right? Forward is the z axis. Anyway, try this

 if (Physics.Raycast (transform.position, transform.forward, out hit, 10)) {
Comment
Add comment · 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
0

Answer by pcdrive · Oct 10, 2016 at 09:40 PM

Hi dude!

I think i know what's wrong, but ima beginner too =P. I dont really understand what you wanted to do. If you wanted to check raycast in the x axis from the object or front of the object. but its almsot the same. just use transfrom.direction for the forward or trunk it more to get its x value to check the x axis =).

Sorry for poor english... not my native.

why you want to transform the direction? i bet the problem is there. I use raycast in collision avoidance ai system with the same generation. you should jsut simply use the ray construktor as Ray ray = new Ray (gameObject.transform.position, gameObject.transform.forward); and it will make you a ray that will be started at the center of your gameObject and points towards the object forward vector. After that you can check wha it hits easily.

I hope i could help.

Good luck

Comment
Add comment · 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
0

Answer by jrocamora · Oct 11, 2016 at 12:18 AM

transform.TransformDirection doesn't return the transform's world coordinates, just a Vector3 that equates to what direction you want in relation to the object, at least that's what I think because I've had a similar problem to yours before.

Try changing your Vector3 fwd to this:

  Vector3 fwd = transform.position + transform.TransformDirection(Vector3.forward);
 

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 Bunny83 · Oct 11, 2016 at 12:29 AM 0
Share

While it is correct that a direction is just a direction vector which has to put in relation to some point in space, your answer is misleading. He doesn't use his "fwd" variable for the "drawing" only in his actual Raycast line. Physics.Raycast needs a direction and not a position as second parameter.

Also

 transform.TransformDirection(Vector3.forward)

is exactly the same as

 transform.forward
avatar image DylanST · Oct 11, 2016 at 12:59 AM 0
Share

I changed "fwd" to transform.forward, and it works fine now. Thank you.

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

7 People are following this question.

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

Related Questions

Transform Object over Players head before destroying 0 Answers

How to have an object some distance above another object Locally? 1 Answer

c# How do I set up continuous raycast collision detection? 1 Answer

Raycast Mouse Click On Specific Objects Only 0 Answers

Creating 2d image map of objects based off 3d space colliders Unity C# 0 Answers


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