人物根据镜头方向转身
float ver = 0;
float hor = 0;
void Rotating (float hor, float ver)
{
//获取方向
Vector3 dir = new Vector3 (hor,0,ver);
float y = Camera.main.transform.rotation.eulerAngles.y;
dir = Quaternion.Euler(0, y, 0) * dir;
//将方向转换为四元数
Quaternion quaDir = Quaternion.LookRotation(dir,Vector3.up);
//缓慢转动到目标点
transform.rotation = Quaternion.Lerp(transform.rotation,quaDir,Time.fixedDeltaTime*turnspeed);
}
void FixedUpdate()
{
if (hor != 0 || ver != 0)
{
//转身
Rotating(hor, ver);
}
}
人物根据镜头方向移动
bool Move()
{
hor = Input.GetAxis("Horizontal");
ver = Input.GetAxis("Vertical");
Vector3 targetDirection = new Vector3(hor, 0, ver);
float y = Camera.main.transform.rotation.eulerAngles.y;
targetDirection = Quaternion.Euler(0, y, 0) * targetDirection;
if (Input.GetKey(KeyCode.W))
{
GetComponent<Animator>().SetBool("Walk", true);
transform.Translate(targetDirection * Time.deltaTime * 1, Space.World);
return true;
}
if (Input.GetKey(KeyCode.S))
{
GetComponent<Animator>().SetBool("Walk", true);
transform.Translate(targetDirection * Time.deltaTime * 1, Space.World);
return true;
}
if (Input.GetKey(KeyCode.A))
{
GetComponent<Animator>().SetBool("Walk", true);
transform.Translate(targetDirection * Time.deltaTime * 1, Space.World);
return true;
}
if (Input.GetKey(KeyCode.D))
{
GetComponent<Animator>().SetBool("Walk", true);
transform.Translate(targetDirection * Time.deltaTime * 1, Space.World);
return true;
}
return false;
}