For some reason, my code is not working... I have no idea why. can someone help? I am using a switch statement to control my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Vector3 pos = transform.position;
string state = "idle";
float vx = 0f;
float vy = 0f;
float playerSpeed = 2f * Time.deltaTime;
switch (state) {
case "idle":
vx = 0;
vy = 0;
if (Input.GetKey (KeyCode.A)) state = "left";
if (Input.GetKey (KeyCode.D)) state = "right";
if (!Input.GetKey (KeyCode.D) && !Input.GetKey (KeyCode.A)) state = "idle";
break;
case "left":
vx = -1 * playerSpeed;
vy = 0;
if (Input.GetKey (KeyCode.A)) state = "left";
if (Input.GetKey (KeyCode.D)) state = "right";
if (!Input.GetKey (KeyCode.D) && !Input.GetKey (KeyCode.A)) state = "idle";
break;
case "right":
vx = playerSpeed;
vy = 0;
if (Input.GetKey (KeyCode.A)) state = "left";
if (Input.GetKey (KeyCode.D)) state = "right";
if (!Input.GetKey (KeyCode.D) && !Input.GetKey (KeyCode.A)) state = "idle";
break;
}
vx += pos.x;
vy += pos.y;
pos += transform.position;
}
}
The console has no errors, and I can not see any error with my code...
Please help!
any answers are greatly appreciated.
Thanks.