Raycasting - Help please

Ok, So I am working on an RTS Game, And I have Made a script that can Select my Units Via drawing a rectangle And it works Perfectly. However, I Was trying to make the Single Target Selection Using Raycasting and Integrate it with my “Selection Manager” Script but I keep Failing. Please Keep in Mind I am a noob so bite-Size Answers would be very much appreciated.

Thanks in Advance!

I use 3 Scripts for this.

  1. Unit Selector, the problem is with this script. Named: “Mouse Point”

  2. Selection Manager. Named “Selection Manager”

  3. A Script On my Units that Indicates they Have been selected with Some Art. Named: “Unit”

  4. This is how my Rectangular Script Works.

1)----------------------------------

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MousePoint : MonoBehaviour
{
      

    RaycastHit hitInfo;
 

    void Update()
    {

        if (Input.GetMouseButtonDown(0))
        {
            callSelectionManager();
        }

        if (Input.GetMouseButtonUp(0))
        {
            callSelectionManager();
        }


    }


    void callSelectionManager()
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hitInfo;

        foreach (Unit myUnits in SelectionManager.unitList)
        {
     

            if (Input.GetMouseButtonDown(0))
            {
                if (Physics.Raycast(ray, out hitInfo, Mathf.Infinity))
                {
                    if (hitInfo.collider.tag == "Player1" && hitInfo.collider.gameObject == myUnits) // I tried (hitInfo.collider.tag == "Player1" && hitInfo.collider.gameObject == myUnits) and 100 other things with no luck
                    {

                        //****This is where I am stuck****

                        myUnits.SetSelector(true);
                        Debug.Log(hitInfo.collider.name);
                    }

                }
            }

        }
                             
    }


}

2)---------------------------------------------------

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SelectionManager : MonoBehaviour
   

{
    public static List<Unit> unitList = new List<Unit>();         
}

3)-------------------------------------------------

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Unit : MonoBehaviour
{

    public GameObject selectorIndicator;

    void Start()
    {
      
        SetSelector(false);                            
        SelectionManager.unitList.Add(this);       

    }

    public void SetSelector(bool on)
    {
        selectorIndicator.SetActive(on);
    }
             
}
  1. A fraction of My rectangular Script---------

foreach (Unit myUnits in SelectionManager.unitList)
{

if (selectionRect.Contains(Camera.main.WorldToScreenPoint(myUnits.transform.position)))
{
myUnits.SetSelector(true);
}
}

Having your “click selection” and “rectangle selection” in two different scripts is going to make things very difficult. What you want to do is to do the rectangle selection logic with the input (in terms of clicking and dragging), and when the mouse button is raised, at that point decide if you’ve moved enough to do a drag select. If so, do your #4 code, if not, do your #1 code.

And, you don’t really need to raycast TBH. You could just do the same sort of thing as you’re doing for a drag-select, except use a distance check instead of Rect.Contains. Raycast selections are often too finicky for RTS games.

@StarManta , Thank you For reply, I tried it on the same script. thats not really my Issue atm, the Issue I have is that this line of code doesn’t work “if (hitInfo.collider.tag == “Player1” && hitInfo.collider.gameObject == myUnits)”, I tried many other lines also. but I cant seem to pin the right one down. I divided the scripts so that I can Isolate the Issue.

This However Sounds Interesting. please expand, How would I go about doing a distance check if my mouse isnt moving at all? would "if(Input.GetMouseButton(0)) " even activate if the Mouse doesn’t move?

I would still love to know how to do it both ways because I am learning, thats the main purpose of this project.

Input.GetMouseButtonDown returns true if the mouse button is held, regardless of movement. You can just loop through the units and do a distance check between Input.mousePosition and Camera.main.WorldToScreenPoint(myUnits.transform.position)

@StarManta , Thank you Very Much, I will give this a shot.

@StarManta Love you bro, I did it your way and it worked. The Min Distance can be tricky though Because every unit has a different size. I wounder if its possible to calculate the distance from the surface of the collider as appose to the centre or something like that.

Problem is, If I want to select a “man unit”, I have to set the distance to something small, and the is a problem because if they use that same distance to select a tank, it wont be selected Unless they click bang in the middle. On the other Hand if I make the Min distance Large to Solve the problem with selecting a tank, it will also cause the player to select a “man unit” without even clicking on the unit.

@StarManta , Hey Bro, After using this method I have encountered new Problems. which I posted In another thread. I would love it if you can take a look at it once you get a chance!

Btw, I May start making youtube videos (showing what I learn) and I will give credit where credit is due :smile: