- Home /
 
Help with deselection please
Im trying to learn how to make a RTS. The problem is that i set Left Click deselect all. I would like to give Left Click another purpose, so i want to deselect when clicking Right Click on "Ground" My code so far:
 private void MouseActivity() {
     if(Input.GetMouseButtonDown(0)) LeftMouseClick();
     else if(Input.GetMouseButtonDown(1)) RightMouseClick();
 }
 private void LeftMouseClick() {
     if(player.hud.MouseInBounds()) {
         GameObject hitObject = FindHitObject();
         Vector3 hitPoint = FindHitPoint();
         if(hitObject && hitPoint != ResourceManager.InvalidPosition) {
             if(player.SelectedObject) player.SelectedObject.MouseClick(hitObject, hitPoint, player);
             else if(hitObject.name != "Ground") {
                 WorldObject worldObject = hitObject.transform.root.GetComponent<WorldObject>();
                 if(worldObject) {
                     //we already know the player has no selected object
                     player.SelectedObject = worldObject;
                     worldObject.SetSelection(true);
                 }
             }
         }
     }
 }
 private void RightMouseClick() {
     if(player.hud.MouseInBounds() && !Input.GetKey(KeyCode.LeftAlt) && player.SelectedObject) {
         player.SelectedObject.SetSelection(false);
         player.SelectedObject = null;
     }
 }
 private GameObject FindHitObject() {
     Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     RaycastHit hit;
     if(Physics.Raycast(ray, out hit)) return hit.collider.gameObject;
     return null;
 }
 private Vector3 FindHitPoint() {
     Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     RaycastHit hit;
     if(Physics.Raycast(ray, out hit)) return hit.point;
     return ResourceManager.InvalidPosition;
 }
 
               }
Answer by QuantumCookies · Apr 13, 2018 at 05:54 PM
Not entirely sure what is being asked, but a simple solution to this:
     private void RightMouseClick()
     {
         if (player.hud.MouseInBounds())
         {
             GameObject hitObject = FindHitObject();
             Vector3 hitPoint = FindHitPoint();
             if (hitObject && hitPoint != ResourceManager.InvalidPosition)
             {
                 if (!Input.GetKey(KeyCode.LeftAlt) && player.SelectedObject)
                 {
                     player.SelectedObject.SetSelection(false);
                     player.SelectedObject = null;
                 }
                 else if (hitObject.name != "Ground")
                 {
                     WorldObject worldObject = hitObject.transform.root.GetComponent<WorldObject>();
                     if (worldObject)
                     {
                         //we already know the player has no selected object
                         player.SelectedObject = worldObject;
                         worldObject.SetSelection(true);
                     }
                 }
             }
         }
     }
 
               The above may be completely wrong but I'm basing it off your code though I did not test it. And then put your own function in the left click.
Personally I would take this approach to selection in an RTS, I edited this selection script a while ago when making an RTS game.
 using System.Collections.Generic;
 using UnityEngine;
 
 public class ExampleSelection : MonoBehaviour {
 
     public List<Unit> SelectedUnits; //This would be the units in the rts change this to a normal Unit if your selecting a single char
 
     public GameObject TerrainObj; //This could be a ground object or a terrain object
     public LayerMask RaycastLayerMask; //The layers used for selection
 
     //For Raycasting
     RaycastHit Hit;
     Ray RayCheck;
 
     void Update()
     {
         if(Input.GetMouseButtonDown(1) || Input.GetMouseButtonDown(0))
         {
             RayCheck = Camera.main.ScreenPointToRay(Input.mousePosition);
             if (Physics.Raycast(RayCheck, out Hit, 80.0f, RaycastLayerMask.value))
             {
                 GameObject HitObj = Hit.transform.gameObject; //If the the ray actually hit an object
 
                 //RIGHT CLICK
                 if (Input.GetMouseButtonDown(1))
                 {
                     //I'm using a tag for the example I would recommend GetComponent()
                     if(HitObj.tag == "Unit")
                     {
                         UnitAction(); //Do an action based on the right click usually when units are currently selected.
                     }
                     else if (HitObj == TerrainObj)
                     {
                         MoveUnits(Hit.point); //Move all currently selected units
                     }
                 }
                 else if (Input.GetMouseButtonDown(0)) //LEFT CLICK
                 {
                     DeselectAll(); //Deselecting you currently selected to
 
                     if(HitObj != null)
                     {
                         if(HitObj.tag == "Unit")
                         {
                             SelectUnit(HitObj.GetComponent<Unit>()); //After deselecting all the units you will reselect this specific unit
                         }
                     }
                 }
             }
         }
     }
 
     void DeselectAll()
     {
         //Deselect all units
     }
 
     void UnitAction()
     {
         if(SelectedUnits.Count > 0)
         {
             //Do something here like attacking enemy units
         }
     }
 
     void MoveUnits(Vector3 pos)
     {
         //Move all selected units to a point
     }
 
     void SelectUnit(Unit unit)
     {
         //Select the unit
     }
 }
 
               This selection is a very basic version. I hope this is what you were looking for, please reply if you have any issues or understanding, as I'm still a bit unclear on the question.
Your answer