- Home /
Not getting right value; ScreenPointToRay
I am creating an RTS Move system, where selected units will move to mouse click location. I have created a ray from my mouse click and I'm getting an incorrect value for RaycastHit. Please look over my code and tell me if I'm doing it wrong.
 using UnityEngine;
 using System.Collections;
 
 public class UserInput : MonoBehaviour {
 
     Vector2 beginMousePos;
     Vector2 currMousePos;
     bool mouseIsSet = false;
     RaycastHit hit;
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         if(Input.GetMouseButtonDown (0) & !mouseIsSet)
         {
             mouseIsSet = true;
             beginMousePos = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
         }
         else if(Input.GetMouseButtonUp (0) & mouseIsSet)
         {
             mouseIsSet = false;
             currMousePos = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
             UnitManager.FindSelectedUnits(beginMousePos, currMousePos);
         }
         if(Input.GetMouseButtonDown (1))
         {
             Debug.Log ("(" + currMousePos.x + "'" + currMousePos.y + ") (" 
                        + beginMousePos.x + "'" + beginMousePos.y + ")");
 
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             if (Physics.Raycast (ray, out hit))
             {
                 UnitManager.MoveSelectedUnits (hit.transform.position);
             }
         }
     }
     
 }
 
What are you trying to hit with the ray? If it's the ground, does the ground have a collider on it? Are there any colliders between your camera and the ground? Try printing out hit.collider.name or hit.collider.tag (and check that hit.collider.tag is a tag that you are interested in hitting).
I figured it out a $$anonymous$$ute ago... Yep the ground and then I wanted the location of the hit, but ins$$anonymous$$d I was getting the location of my plane, when I realized this, it was a no brainer that hit.tranform.position was not what I wanted, after checking reference I fount hit.point was the accurate vector3 to obtain from my raycasthit... Thanks for you help though, I really appreciate it.
Your answer
 
 
             Follow this Question
Related Questions
Shoot off multiple raycasts from 1 object? 2 Answers
How do I keep functionality of LookAt script whilst using it in a 3dGUI camera layer? 0 Answers
Stop the lasers from keep bouncing 1 Answer
Door only opening once on Raycast 1 Answer
Raycast detects box collider, but not capsule collider 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                