- Home /
Why is it only using one mouse input?
Ok so here is the script I have so far and it is suppose to be selecting and deselecting the unit by clicking on the child objects collider when clicking the left mouse button but it only does the first mouse click option for instantiating a target. I'm sure its just a formatting error on my part so any help to this would be wonderful. I'm very new to this and I'm stumped.
RaycastHit hit;
public GameObject Target;
private GameObject targObj;
private float raycastLength = 500;
public static GameObject CurrentlySelectedUnit;
private static Vector3 mouseDownPoint;
bool objExists;
bool leftRightMouseCheck;
void awawke(){
mouseDownPoint = Vector3.zero;
}
void Update () {
if (Input.GetMouseButtonDown (1)) {
if (objExists == false) {
//create targObj if not already created
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, out hit, raycastLength)) {
Debug.DrawRay (ray.origin, ray.direction * raycastLength, Color.yellow);
if (hit.collider.name == "TerrainMain") {
//Creates Object if it doesn't exist
targObj = Instantiate (Target, hit.point, Quaternion.identity) as GameObject;
targObj.name = "TargetInstantiated";
Debug.Log ("Created");
objExists = true;
}
}
} else {
Destroy (targObj);
Debug.Log ("Destroyed");
//recreate targObj after destroying previous
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, out hit, raycastLength)) {
Debug.DrawRay (ray.origin, ray.direction * raycastLength, Color.yellow);
if (hit.collider.name == "TerrainMain") {
//Creates Object if it doesn't exist
targObj = Instantiate (Target, hit.point, Quaternion.identity) as GameObject;
targObj.name = "TargetInstantiated";
Debug.Log ("Created");
objExists = true;
}
}
}
}
//store mouse point
if (Input.GetMouseButtonDown (0)) {
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, out hit, raycastLength)) {
if (hit.collider.name == "TerrainMain") {
mouseDownPoint = hit.point;
if (Input.GetMouseButtonUp (0) && DidUserClickLeftMouse (mouseDownPoint)) {
DeselectGameobjectIfSelected ();
if (hit.collider.transform.FindChild ("character")) {
Debug.Log ("Found a Unit");
//are we selecting a different
if (CurrentlySelectedUnit != hit.collider.gameObject) {
//activate the selector
GameObject SelectedObj = hit.collider.transform.FindChild ("character").gameObject;
SelectedObj.SetActive (true);
//deactivate currently selected
if (CurrentlySelectedUnit != null) {
CurrentlySelectedUnit.transform.FindChild ("character").gameObject.SetActive (false);
//replace currently selected unit
CurrentlySelectedUnit = hit.collider.gameObject;
}
} else {
//if this object is not a unit
DeselectGameobjectIfSelected ();
}
}
}
} else {
if (Input.GetMouseButtonUp (0) && DidUserClickLeftMouse (mouseDownPoint)) {
DeselectGameobjectIfSelected ();
}
}
}
}
}
#region helper functions
//check if user clicked mouse
public bool DidUserClickLeftMouse(Vector3 hitPoint)
{
float clickZone = 1.3f;
if (
(mouseDownPoint.x < hitPoint.x + clickZone && mouseDownPoint.x > hitPoint.x - clickZone) &&
(mouseDownPoint.y < hitPoint.y + clickZone && mouseDownPoint.y > hitPoint.y - clickZone) &&
(mouseDownPoint.z < hitPoint.z + clickZone && mouseDownPoint.z > hitPoint.z - clickZone)
)
return true;
else
return false;
}
static void DeselectGameobjectIfSelected()
{
if (CurrentlySelectedUnit != null) {
CurrentlySelectedUnit.transform.FindChild ("character").gameObject.SetActive(false);
CurrentlySelectedUnit = null;
}
}
#endregion
}
if (Input.Get$$anonymous$$ouseButtonDown (0)) {
...
if (Input.Get$$anonymous$$ouseButtonUp (0)) {
means that the mouse left button needs to be pressed and released at the same time. Also, supposing that the unit is being clicked, it is referred by hit.collider
, so most likely
(hit.collider.name == "Terrain$$anonymous$$ain")
will be false and
DidUserClickLeft$$anonymous$$ouse (mouseDownPoint)
will be true.
Are you just trying to detect a left click or is it something more complex ?
that bool leftrightmousecheck isnt being used and i forgot to remove it. Im trying to get it so i can select the unit by the left mouse click and then use this function (by clicking the right mouse button) to instantiate the pathfinding target for the unit to go to.
`if (Input.Get$$anonymous$$ouseButtonDown (1)) {
if (objExists == false) {
//create targObj if not already created
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, out hit, raycastLength)) {
Debug.DrawRay (ray.origin, ray.direction * raycastLength, Color.yellow);
if (hit.collider.name == "Terrain$$anonymous$$ain") {
//Creates Object if it doesn't exist
targObj = Instantiate (Target, hit.point, Quaternion.identity) as GameObject;
targObj.name = "TargetInstantiated";
Debug.Log ("Created");
objExists = true;
}
}`
`} else {
Destroy (targObj);
Debug.Log ("Destroyed");
//recreate targObj after destroying previous
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, out hit, raycastLength)) {
Debug.DrawRay (ray.origin, ray.direction * raycastLength, Color.yellow);
if (hit.collider.name == "Terrain$$anonymous$$ain") {
//Creates Object if it doesn't exist
targObj = Instantiate (Target, hit.point, Quaternion.identity) as GameObject;
targObj.name = "TargetInstantiated";
Debug.Log ("Created");
objExists = true;
}
}
}
}
`
but it wont let me select the unit when i click on the "character" collider. "character" being a child to the whole unit. it only lets me instantiate the target. and it wont do both.
im sure it's some kind of issue with how i set it up because i did get the selecting to work but then the instantiating target part did not work.
Your answer
![](https://koobas.hobune.stream/wayback/20220613145916im_/https://answers.unity.com/themes/thub/images/avi.jpg)