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 /
This question was closed May 08, 2017 at 02:34 PM by salamander555 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by salamander555 · May 07, 2017 at 09:18 AM · rotationraycasttransformanglemouseposition

Making an object follow the mouse in 3D world

Hello,

This question isn't much about an abject following the mouse cursor n a 3D world but that is the effect I want to get. I'm currently making a way to build your custom vehicle and to do this I'm mainly using Physics.Raycast to check if there's another block with a collider where you can place this block against. This worked aslong as I used my transform.forward as the angle for the raycast input. This is what I was using.

 if (Physics.Raycast(transform.position, transform.forward, out hit, 50f))
         {
             if (GameObject.Find("blockPlacing") != null)
             {
                 if (hit.collider != GameObject.Find("blockPlacing").GetComponent<Collider>())
                 {
                     GameObject.Find("blockPlacing").transform.position = new Vector3(Mathf.Round(hit.point.x), Mathf.Round(hit.point.y+0.4f), Mathf.Round(hit.point.z));
                 }
             }
             else
             {
                 Instantiate(transformOfBlockMaking, new Vector3(hit.point.x, hit.point.y, hit.point.z), new Quaternion(0, 0, 0, 0), GameObject.Find("Blocks").transform).name = "blockPlacing";
             }
 
             if (Input.GetMouseButtonUp(0))
             {
                 GameObject.Find("blockPlacing").name = "block(0)";
             }
         }
         else
         {
             Destroy(GameObject.Find("blockPlacing"));
         }

This works fine (note: some coding line might look a bit strange but ignore that) but what I know want to do because this way of making you vehicle was a bit annoying. Is that the raycast doesn't use tranform.forward but that is uses the "angle of the mouse in the world position" look at the drawing below. So I want to use the green line as my angle of the raycast instead of Transform.forward <-- this my question alt text

Things I tried to calculate this angle: So I did the 'basics' and I calculated the mouseAngle on my canvas

  mouseInScreenAngle.x = (Input.mousePosition.x / Screen.width -0.5f) * Camera.main.fieldOfView
 
  mouseInScreenAngle.y = (Input.mousePosition.y / Screen.height -0.5f) * Camera.main.fieldOfView

The Results of this is That I have an angle between [-30°,30°] and after this step I'm stuck. so if someone could help me complete this or if you know another way comment below pls.

unity-vraag-mouseposition.png (27.5 kB)
Comment
Add comment · Show 1
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 Masterio · May 07, 2017 at 06:56 PM 1
Share

Check it out ( i mean example project from my answer):

http://answers.unity3d.com/questions/1324308/left-click-once-does-an-action-left-click-again-do.html?childToView=1324820#answer-1324820

and this is what you need:

 Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 100f, 1 << Layer$$anonymous$$ask.NameToLayer(GROUND_LEAYER_NA$$anonymous$$E)))

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

108 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Problems caused by non-unique Euler angle solutions 1 Answer

How can I fix this randomly rotating gun to an unknown degrees? 1 Answer

Rotate angle 0 Answers

Rotate an object around another object at an angle from the X axis? 1 Answer

Make an object move to a given point by rotating to the correct direction first. 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