So at the moment, the script is supposed to be able to detect if the unit is clicked- and then select it, after that, two buttons come up “move” and “attack”. If move is selected, the player is supposed to be able to issue ONE move command by clicking where he wants the unit to move. Unfortunately, I’ve tried several things to prevent the unit from being able to move after a move order has already been issued, but to no avail. Hopefully someone here can help, because I’m all out of ideas.
var Selected : boolean;
var isAtkButtonVisible : boolean = false;
var isMvButtonVisible : boolean = false;
var isMoving : boolean = false;
var unitMoveButton : boolean = false;
var moveSpeed:float = 20;
private var targetPosition:Vector3;
private var targetDistance:float;
var speed = 3;
var buttonRectangle : Rect = Rect(100, 100, 100, 50);
function Start(){
Selected = false;
isAtkButtonVisible = false;
isMvButtonVisible = false;
targetPosition = transform.position;
}
function OnMouseOver(){
if(Input.GetMouseButtonDown(0)){
Selected = true;
isAtkButtonVisible = true;
isMvButtonVisible = true;
// Commands();
}
}
function MoveUnit(){
targetDistance = Vector3.Distance(targetPosition, transform.position);
if(targetDistance < 1){ // prevents shaking when it reaches location
moveSpeed = 0;
}
else if(targetDistance > 1){
moveSpeed = 5;
}
if(Input.GetKeyDown(KeyCode.Mouse0) && (unitMoveButton))
{
var playerPlane = new Plane(Vector3.up, transform.position);
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hitdist:float = 0.0;
if (playerPlane.Raycast (ray, hitdist)) {
targetPosition = ray.GetPoint(hitdist);
}
}
if(targetDistance > 1){ // Prevents code running when it doesn't need to
transform.position += (targetPosition - transform.position).normalized * moveSpeed * Time.deltaTime;
}
transform.position.y = 1;
}
function Update(){
if(Input.GetKeyDown(KeyCode.Escape)){
Selected = false;
isAtkButtonVisible = false;
isMvButtonVisible = false;
}
if(isMoving){
MoveUnit();
}
}
function OnGUI(){
if(isAtkButtonVisible)
{
if(GUI.Button(Rect(Screen.width/6 - 75,Screen.height/1.1 - 25,150,50),"Attack"))
{
isAtkButtonVisible = false;
isMvButtonVisible = false;
}
}
if(isMvButtonVisible){
if(GUI.Button(Rect(Screen.width/4,Screen.height/1.1 - 25,150,50),"Move") && (unitMoveButton == false))
{
isMvButtonVisible = false;
isAtkButtonVisible = false;
unitMoveButton = true;
isMoving = true;
}
}
}