- Home /
Re-enabling Grid Move script after re-selection
Hello everyone!
I am having an issue re-enabling the grid move script after i have deselected and then re-selected the same object. Step by step, this is what happens
I select the object and my Grid Move script is enabled, which fires a while loop waiting for input from the player
I deselect the object and I can see via the console log that the second while loop stops, which is exactly what i want
I re-select the object, but the while loops do not fire back up again. I have the conditions of the while loop set as while(this.enabled && mp>0) Another problem i have is that it is subtracting one movement point (mp) when i deselect, even if i do not hit an arrow key, which i am guessing is happening because the entire while loop will fire, which ends in a mp-- command that will fire regardless.
here is my code:
var walkSpeed : float = 1.0;
var runSpeed : float = 2.0;
var gridSize : int = 1;
enum Orientation {Horizontal, Vertical}
var gridOrientation = Orientation.Horizontal;
var allowDiagonals = false;
var correctDiagonalSpeed = true;
private var input = Vector2.zero;
var character: Character;
var selected: boolean = false;//can be deleted when debug done
var mp: int;
function Start ()
{
character = GetComponentInChildren(Character);
mp = character.getMovementPoints();
var myTransform = transform;
var startPosition : Vector3;
var endPosition : Vector3;
var t : float;
var tx : float;
var moveSpeed = walkSpeed;
//if(this.enabled)
//{
while (this.enabled && mp > 0)
{
Debug.Log("first while loop passes");
while (this.enabled && input == Vector2.zero)
{
Debug.Log("second while loop passes");
GetAxes();
tx = 0.0;
yield;
}
transform.forward = Vector3.Normalize(new
Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical")));
startPosition = myTransform.position;
endPosition = gridOrientation == Orientation.Horizontal?
Vector3(Mathf.Round(myTransform.position.x), 0.0,
Mathf.Round(myTransform.position.z)) +
Vector3(System.Math.Sign(input.x)*gridSize, 0.0,
System.Math.Sign(input.y)*gridSize)
:
Vector3(Mathf.Round(myTransform.position.x),
Mathf.Round(myTransform.position.y), 0.0) +
Vector3(System.Math.Sign(input.x)*gridSize, System.Math.Sign(input.y) *gridSize, 0.0);
t = tx;
while (t < 1.0)
{
Debug.Log("third while loop passes");
//moveSpeed = Input.GetButton("Run")? runSpeed : walkSpeed;
t += Time.deltaTime * (moveSpeed/gridSize) *
(correctDiagonalSpeed && input.x != 0.0 && input.y != 0.0? .7071 : 1.0);
myTransform.position = Vector3.Lerp(startPosition,
endPosition, t);
yield;
}
tx = t - 1.0; // Used to prevent slight visual hiccups on "grid lines" due to Time.deltaTime variance
GetAxes();
//added to subtract one movement from moved character
yield;
}
//}
mp--;
}
function Update()
{
}
function GetAxes ()
{
if(this.enabled)
{
input = Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
if (allowDiagonals)
return;
if (Mathf.Abs(input.x) > Mathf.Abs(input.y))
{
input.y = 0.0;
}
else
{
input.x = 0.0;
}
}
}
While typing this out i realized it could be because there is no update function present, its all in a start function which i know is best for while loops. And im questioning if this entire deal would be solved if i stuck if(this.enabled) in the update function, but what would i call underneath? Can you call Start()?
Also, that will not fix my movement points issue (mp).
Any thoughts on how to fix that issue, and if there is a more efficient way of doing this I would love to know. I have racked my brain on this for a day and I'm fairly positive i just need to stick an if statement in update checking if enabled, but again not sure if i can call start from that.
Thanks for any help and advice you guys can give me! :-)
Your answer