• 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Vetm · Dec 14, 2014 at 02:13 PM · ray-casting

Raycasting with global position

I want to shoot a Raycast from an object to a Vector 3. The Raycast should always shoot in the direction of the Vector3 even if I move the Object. This is the code I use:

 Debug.DrawRay (transform.position, new Vector3(0,10,0), Color.green);


However, when I move the Object that casts the Ray, the ray shoots to the local 0,10,0 and not to the global 0,10,0.

How do I make it shoot in the direction of a global Vector3?

alt text alt text

1.png (74.6 kB)
2.png (72.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

2 Replies

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

Answer by Vetm · Dec 14, 2014 at 03:16 PM

I found the answer myself. Since the second argument in Debug.DrawRay is a direction and not a position you have to calculate to direction where the ray should point at. This is how you do it.

 Debug.DrawRay (transform.position, -(transform.position-new Vector3(0,10,0)), Color.green);



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 Professor Snake · Dec 14, 2014 at 03:18 PM 0
Share

Fantastic. Although i don't think calling new Vector3(0,10,0) every frame is all that optimal since, to my knowledge, it would lead to a memory leak. You should replace it with just Vector3(0,10,0)

avatar image Vetm · Dec 14, 2014 at 03:23 PM 0
Share

The new Vector3 is just a placeholder for the Player Position. I thought it would sound more complicated if I added the Player Position and it would sound more specific so others with the same problem couldn't relate it to their problem.

avatar image
0

Answer by Professor Snake · Dec 14, 2014 at 03:05 PM

The second argument in Debug.DrawRay is used as a direction, not a position. If you'd like to draw a line between an object and a point, use Debug.DrawLine instead. If you want it to point towards said point and have a variable length, use Debug.DrawRay(origin.position,(target.position-origin.position).normalized*length);

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

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

move player to finger position 1 Answer

Cast shadow in Unity 2 Answers

Can i raycast against a specific object without using layers? 1 Answer

Lighting up all triangles between two points in a mesh? 1 Answer

Make a Raycast cause a Knock back 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