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 /
  • Help Room /
This question was closed Sep 05, 2015 at 11:41 AM by Frozaken for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Frozaken · Sep 02, 2015 at 09:35 AM · raycastquaternionraycastingmouseposition

My Ray is going the wrong way

i have a top down view game, where you can move arounds with w,a,s,d and you make the character look around with the mouse(the camera is stationary), i wanted to shoot a ray to collide with the terrain, to then create a point so i can calculate my characters rotation. When i draw the ray in the debug mode, it just shoots backwards from the camera. How do i make it shoot to the ground?

     #pragma strict
     
     var Cam : Transform;
     
     function Update()
     {
 Debug.DrawRay (Cam.position, Input.mousePosition, Color.green);
     }
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 Scribe · Sep 02, 2015 at 11:53 AM 0
Share

Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); will help

avatar image Frozaken · Sep 02, 2015 at 09:52 PM 0
Share

i tried this now but diddnt work: var distancefcam : float; var cam : Camera; var hit: RaycastHit; var camPos : Transform;

 function update ()
 {
  Debug.DrawRay(transform.position,transform.TransformDirection(Vector3.forward),Color.green);
  var ray : Ray = cam.ScreenPointToRay(Input.mousePosition);
  if (Physics.Raycast(ray,hit)) 
  {
   print("hit something at "+hit.distance+" meters");
   distancefcam = hit.distance;
  }
 }
avatar image Frozaken · Sep 02, 2015 at 09:52 PM 0
Share

and dont $$anonymous$$d the debug thing..

avatar image Scribe · Sep 03, 2015 at 09:16 AM 1
Share

If you have setup cam right, that should work as intended, look at this page, and specifically the last example script (scroll all the way down), it is almost exactly what you are currently doing :o

avatar image Frozaken · Sep 03, 2015 at 10:34 AM 1
Share

Thanks alot! works like a charm! :)

1 Reply

  • Sort: 
avatar image
0

Answer by EvilTak · Sep 02, 2015 at 10:45 AM

You will have to use Camera.ScreenToWorldPoint as Input.mousePosition contains the mouse position in screen space which you will need to convert to world sppace. You will have to note that the z component of the parameter vector in the ScreenToWorldPoint method contains the distance at which the point should be from the camera. So for example if your camera's z position is at -10 and your world is at z 0, the z component of the vector you pass to the method has to be 10.

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 Frozaken · Sep 02, 2015 at 09:32 PM 0
Share

im sorry, but it doesnt work for me, where exactly should i put it? thanks for your help btw!

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Raycast not drawing where i tell it 1 Answer

Shoot towards mouse except mouse y 1 Answer

Can't call raycast on plane to get mouse position? Beginner Question 1 Answer

Clamp vector values to a minimum circular radius around player? 1 Answer

Raycast line renderer not drawing correctly 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