Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
1
Question by Nikke · Feb 07, 2011 at 02:37 PM · cameravectorlook

How to get the look (or forward) vector of the camera?

Hi,

I need to raytrace a line from the camera until the point on the ground the user is looking at. For this I need the look vector of the camera, any idea how I can get it?

Thanks, Nick

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
7
Best Answer

Answer by PrimeDerektive · Feb 07, 2011 at 02:54 PM

Should be the same as any other GameObject, transform.forward.

Unity camera's frustrums always point in the same direction as their transform's forward vector (transform.forward).

So if you do Physics.Raycast(transform.position, transform.forward, hit, length) it will shoot a ray towards whatever is directly in the center of the camera's frustrum.

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 e-bonneville · Feb 07, 2011 at 02:56 PM 2
Share

How have I not heard of that until now?! Well, I guess you learn something new every day. :)

avatar image Nikke · Feb 07, 2011 at 03:24 PM 0
Share

Thank you, that did the trick!

avatar image
0

Answer by e-bonneville · Feb 07, 2011 at 02:54 PM

Use transform.InverseTransformDirection.

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 Jesse Anders · Feb 08, 2011 at 02:03 AM 0
Share

InverseTransformDirection() isn't relevant to the problem (at least not as stated).

avatar image
0

Answer by LarryTheBrave · Mar 23, 2019 at 10:33 PM

Forward in Unity or "Which way this object is considered pointing?" would be along the local Z axis. Parent the object to the camera and have everything 0 except add something to Z. Then it will appear "in front of" the parent object at all times.

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 ChrisP999 · Apr 21 at 04:52 AM

The other answers are all wrong, at least in my usage case, here is the code that works:

 t = _mainCamera.transform; //  CinemachineCameraTarget.transform;
 Vector3 look = t.TransformDirection(Vector3.forward);
 Debug.DrawRay(t.position, look,Color.green,14);
 if (Physics.Raycast(t.position, look, out hit, weaponlength, layerMask)) {
        ...
 }






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.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

move the object where camera look 0 Answers

Input.mousePosition for new networking 0 Answers

How spaces works in unity? 1 Answer

LookAt only when camera position changes 2 Answers

Line Renderer is not being updated in the Game window 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