Hey I Need A little help… I have two SCripts my CamOP and my Units… im new to coding so im still learning by following tuts aswell as implementing my own things as i go so bare with me please… basically when i try and select a unit by either dragging or Simply clicking you cannot click anywhere sometimes it doesnt even work if you dont click 100% correctly… but it does work basically if you could help me click on a unit any part of the mesh would be ideal…
UNITS.CS
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Units : MonoBehaviour
{
public bool selected = false;
//private Vector3 moveToDest = Vector3.zero;
public float floorOffSet = 1;
//Click Select
private bool selectionByClick = false;
// Update is called once per frame
private void Update ()
{ //Place Layer Unit Control Here.
if (Input.GetMouseButton (0))
{
if (!selectionByClick)
{
Vector3 camPos = Camera.main.WorldToScreenPoint (transform.position);
camPos.y = CamOP.InvertMouseY(camPos.y);
selected = CamOP.selection.Contains (camPos);
}
if (selected)
gameObject.GetComponentInChildren<Projector>().enabled = true;
else
gameObject.GetComponentInChildren<Projector>().enabled = false;
}
if (selected && Input.GetMouseButtonUp (1))
{
Vector3 destination = CamOP.GetDestination ();
if (destination != Vector3.zero)
{
gameObject.GetComponent<NavMeshAgent>().SetDestination (destination);
}
}
}
private void OnMouseDown()
{
selectionByClick = true;
selected = true;
}
private void OnMouseUp()
{
if (selectionByClick)
selected = true;
selectionByClick = false;
}
}
CamOP.cs
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class CamOP : MonoBehaviour
{
//Reference to player
private Player User;
//Drag Select
public Texture2D SelectionHightLight = null;
public static Rect selection = new Rect(0, 0, 0, 0);
private Vector3 StartClick = -Vector3.one;
//Move To
private static Vector3 moveToDestination = Vector3.zero;
private static List<string> passables = new List<string>() { "Floor" }; //Make Sure To Name the Terrain Floor
//On Start Find Player
void Start()
{
User = transform.root.GetComponent< Player >();
}
// Update is called once per frame
void Update ()
{
if(User.human) //checks to see if user is HUMAN
{
CheckCamera ();
Cleanup ();
}
}
//Setting up the drag box selection
private void CheckCamera()
{
if (Input.GetMouseButtonDown (0))
StartClick = Input.mousePosition;
else if (Input.GetMouseButtonUp(0))
StartClick = -Vector3.one;
if (Input.GetMouseButton (0))
{
selection = new Rect (StartClick.x, InvertMouseY (StartClick.y), Input.mousePosition.x - StartClick.x, InvertMouseY (Input.mousePosition.y) - InvertMouseY (StartClick.y));
if (selection.width < 0)
{
selection.x += selection.width;
selection.width = -selection.width;
}
if (selection.height < 0)
{
selection.y += selection.height;
selection.height = -selection.height;
}
}
}
//Drag Box Creation
private void OnGUI()
{
if (StartClick != -Vector3.one)
{
GUI.color = new Color(1, 1, 1, 0.5f);
GUI.DrawTexture(selection, SelectionHightLight);
}
}
public static float InvertMouseY(float y)
{
return Screen.height - y;
}
private void Cleanup()
{
if (!Input.GetMouseButtonUp (1))
moveToDestination = Vector3.zero;
}
public static Vector3 GetDestination()
{
if (moveToDestination == Vector3.zero)
{
RaycastHit hit;
Ray r = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast (r, out hit))
{
while (!passables.Contains (hit.transform.gameObject.name))
{
if (!Physics.Raycast(hit.point + r.direction * 0.1f, r.direction, out hit))
break;
}
}
if (hit.transform != null)
moveToDestination = hit.point;
}
return moveToDestination;
}
}