I'm new to unity and coding in general here is my code so far. I'm not sure whats wrong if i could get some help that would be great. i'm trying to make a 2d strategy game where I can move once within the space of 5 seconds. the script is called PlayerController.
#pragma strict
var ph : PlayerController;
private var speed = 10;
private var pos : Vector3;
private var tr : Transform;
function Start()
{
pos = transform.position;
tr = transform;
ph = GetComponent(PlayerController);
ph.enabled = false;
}
function Update()
{
if (Input.GetKey(KeyCode.D) && tr.position == pos)
{
pos += Vector3.right;
}
else if (Input.GetKey(KeyCode.A) && tr.position == pos)
{
pos += Vector3.left;
}
else if (Input.GetKey(KeyCode.W) && tr.position == pos)
{
pos += Vector3.up;
}
else if (Input.GetKey(KeyCode.S) && tr.position == pos)
{
pos += Vector3.down;
}
transform.position = Vector3.MoveTowards(transform.position, pos, Time.deltaTime * speed);
}
function OnlyMoveOnce(runTime : float) {
var timer = 5;
while (timer < runTime) {
ph.enabled = true;
timer += Time.deltaTime;
yield;
}
}
↧