一开始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);
}
}
你这写的好乱啊
if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow)) //前移
{
transform.Translate(Vector3.forward * speed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow)) //后移
{
transform.Translate(Vector3.back * speed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow)) //左移
{
transform.Translate(Vector3.left * speed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow)) //右移
{
transform.Translate(Vector3.right * speed * Time.deltaTime);
}
或者
horizontal = Input.GetAxisRaw("Horizontal");
vertical = Input.GetAxisRaw("Vertical");
if(horizontal!=0 || vertical!=0)
{
transform.Translate(new Vector3(horizontal,0,vertical) * speed * Time.deltaTime);
}
问题出在“||”中
该符号是表示或
在你展示代码的17行、25行
以及21行29行
if条件是一样的
按照逻辑的先后顺序
上下是不能用的
左右是可以用的
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))//重复A 1
{
isVerticalAvailable = true ;
}
if (Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.RightArrow))//重复B 1
{
isVerticalAvailable = false ;
}
if (Input.GetKeyUp(KeyCode.DownArrow) || Input.GetKeyUp(KeyCode.UpArrow))//重复A 2
{
isVerticalAvailable = false ;//上下的最终启用效果为否
}
if (Input.GetKeyUp(KeyCode.LeftArrow) || Input.GetKeyUp(KeyCode.RightArrow))//重复B 2
{
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);
}
}