- Home /
 
The question is answered, right answer was accepted
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 
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.
Check it out ( i mean example project from my answer):
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)))
 
                 Follow this Question
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