- Home /
How to properly set the position clicked by the mouse?
I'm trying to isolate a sistem to move units when selected to a position where the mouse is clicked, so far is selecting apropiatedly, but for some reason the Controller class can't move the selected units where i want them to be move, it's like it doesn't recognize the mouse clicking when i press in some place in the space, to select the target position what exactly is wrong here?
here is the code (this is the controller class):
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class CharacterMovementBySelect : MonoBehaviour {
public GameObject selectedGObject;
public List<GameObject> selectedGObjects = new List<GameObject>();
private Vector3 target;
private bool targetMet;
public float speed = 1.5f;
private Vector3 VectorSelected;
//this is to choose the mode of moving
public bool isMovingForClick = true;
void Update () {
Debug.Log ("Enter update"+"\n VectorSelected: "+VectorSelected+"\n targetMet:"+targetMet+"\n target:"+target+"\n selectedGObjectS:"+selectedGObjects.Count+"\n selectedGObject"+selectedGObject);
if (!isMovingForClick) {
if (Input.GetKey (KeyCode.RightArrow)) {
VectorSelected += Vector3.right;
}
if (Input.GetKey (KeyCode.LeftArrow)) {
VectorSelected += Vector3.left;
}
if (Input.GetKey (KeyCode.UpArrow)) {
VectorSelected += Vector3.up;
}
if (Input.GetKey (KeyCode.DownArrow)) {
VectorSelected += Vector3.down;
}
foreach (GameObject gobject in selectedGObjects) {
gobject.transform.Translate (VectorSelected * speed * 0.1f);
}
if (Input.GetKey (KeyCode.RightArrow) ||
Input.GetKey (KeyCode.LeftArrow) ||
Input.GetKey (KeyCode.UpArrow) ||
Input.GetKey (KeyCode.DownArrow)) {
targetMet = true;
}
}
if (Input.GetMouseButtonDown (0)) {
Debug.Log ("Is Here");
if (isMovingForClick) {
Debug.Log ("Entered GedMouseButton");
target = Camera.main.ScreenToWorldPoint (Input.mousePosition);
target.z = transform.position.z;
Debug.Log ("New Target Position: " + target);
targetMet = false;
}
}
if (!targetMet) {
foreach (GameObject gobject in selectedGObjects) {
Debug.Log (gobject.name + "is moving, or at least it should"+targetMet.ToString());
if (!targetMet) {
Debug.Log ("Still do not arrive there");
gobject.transform.position = Vector3.MoveTowards (gobject.transform.position, target, speed * Time.deltaTime);
if (gobject.transform.position == target) {
Debug.Log ("position == target");
targetMet = true;
break;
}
}
}
}
}
public void setSelectedGObjects(GameObject gobject)
{
selectedGObjects.Add(gobject);
}
}
i already try with OnMouseClick listener and placing the isMovingForClick (boolean) before the Input.GetMouseDown() part with no use.
Comments, Questions, Request for clarification and answer, or in general, anything than help to achieve the wanted results would be much apreciated
Thanks in advance.
Like an addition, all the Debug.log inside the Input.Get$$anonymous$$ouseButtonDown (0) is not been shown at all.
turns out to be an inspector problem, where is$$anonymous$$ovingForClick was set to false and wasn't taking the value of the script, well, me bad, because of that i'm not gonna answer this question.
Thanks in any case