一开始W可以用S用不了,点了AD以后WS都用不了,只能左右动
public float horizontal = 0f;
public float vertical = 0f;
public float speed = 2f;
public Vector3 direction = new Vector3(0, 1, 0);
public bool isVerticalAvailable = false;
private Vector3 playerRotation = Vector3.zero;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
//1 通过键盘按钮按下以及松开,来获取水平/垂直方向的可用性
if (Input.GetKeyDown(KeyCode.DownArrow) || Input.GetKeyDown(KeyCode.UpArrow))
{
isVerticalAvailable = true ;
}
if (Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.RightArrow))
{
isVerticalAvailable = false ;
}
if (Input.GetKeyUp(KeyCode.DownArrow) || Input.GetKeyUp(KeyCode.UpArrow))
{
isVerticalAvailable = false ;
}
if (Input.GetKeyUp(KeyCode.LeftArrow) || Input.GetKeyUp(KeyCode.RightArrow))
{
isVerticalAvailable = true ;
}
//通过Input控制器判断方向
horizontal = Input.GetAxisRaw("Horizontal");
vertical = Input.GetAxisRaw("Vertical");
//垂直方向
if (isVerticalAvailable)
{
if (vertical > 0) //上
{
playerRotation = Vector3.zero;
}
if (vertical < 0) //下
{
playerRotation = new Vector3(0,0,180);
}
}
//水平方向
if (!isVerticalAvailable)
{
if (horizontal > 0) //右
{
playerRotation = new Vector3(0, 0, -90);
}
if (horizontal < 0) //左
{
playerRotation = new Vector3(0, 0, 90);
}
}
//重置游戏方向
this.transform.rotation = Quaternion.Euler(playerRotation);
if (horizontal!=0 || vertical!=0)
{
this.transform.Translate(direction * speed * Time.deltaTime);
}
}