Hi folks,
I’m trying to make the NPC in my game move by using a switch statement, however it’s not working.
First, I use a function to pick a number from 0 to 3 then I use the switch statement:
void Start()
{
int randomNumber = Random.Range(0, 3);
switch (randomNumber)
{
case 0:// the npc moves towards
targetPosition.y = 1.4f;
targetPosition.x = this.transform.position.x;
targetPosition.z = this.transform.position.z - 3;
this.transform.position = Vector3.MoveTowards(transform.position, targetPosition, velocity * Time.deltaTime);
break;
case 1://the npc moves back
targetPosition.y = 1.4f;
targetPosition.x = this.transform.position.x;
targetPosition.z = this.transform.position.z + 3;
this.transform.position = Vector3.MoveTowards(transform.position, targetPosition, velocity * Time.deltaTime);
break;
case 2://the npc moves right
targetPosition.y = 1.4f;
targetPosition.x = this.transform.position.x - 3;
targetPosition.z = this.transform.position.z;
this.transform.position = Vector3.MoveTowards(transform.position, targetPosition, velocity * Time.deltaTime);
break;
case 3://the npc moves left
targetPosition.y = 1.4f;
targetPosition.x = this.transform.position.x + 3;
targetPosition.z = this.transform.position.z;
this.transform.position = Vector3.MoveTowards(transform.position, targetPosition, velocity * Time.deltaTime);
break;
default: break;
}
}
The number is generated, but the NPC doesn’t move. What am I doing wrong here?