键盘控制的旋转:
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
Vector3 dir = new Vector3(h, 0, v);
Quaternion newRotation = Quaternion.LookRotation(dir);
//transform.rotation = Quaternion.Lerp(transform.rotation, newRotation, Time.deltaTime * 6);
transform.rotation = newRotation;
鼠标点击控制的旋转:
void Update () {
if(Input.GetMouseButtonDown(0) && UICamera.isOverUI == false)
{
isClickUp = false;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitInfo;
bool isHave = Physics.Raycast(ray, out hitInfo);
if(isHave)
{
if(hitInfo.collider.tag == Tags.ground)
{
targetPos = new Vector3(hitInfo.point.x, transform.position.y, hitInfo.point.z);
ShowClickEffect(hitInfo.point);
LookAtPos(hitInfo.point);
}
}
}
if(Input.GetMouseButtonUp(0))
{
isClickUp = true;
}
if(!isClickUp)
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitInfo;
bool isHave = Physics.Raycast(ray, out hitInfo);
if (isHave)
{
if (hitInfo.collider.tag == Tags.ground)
{
targetPos = new Vector3(hitInfo.point.x, transform.position.y, hitInfo.point.z);
LookAtPos(hitInfo.point);
}
}
}
}
//生成鼠标点击的特效
void ShowClickEffect(Vector3 Pos)
{
Pos += new Vector3(0, 0.2f, 0);
Instantiate(greenClickEffect, Pos, Quaternion.identity);
}
//设置人物的旋转
void LookAtPos(Vector3 Pos)
{
Pos = new Vector3(Pos.x, transform.position.y, Pos.z);
transform.LookAt(Pos);
}
//
相机被遮住解决方法:
算出相机距离人物的距离,在人物正上方得到该长度的一个点。
在相机与该点的路径上逐次寻找能看望见人物的点。
private void LateUpdate()
{
float length = Vector3.Magnitude(transform.position - player.position);
Vector3 up = player.up * length;
RaycastHit info;
Vector3 temp = player.position - offset;
while(true)
{
Physics.Raycast(temp, player.position - temp, out info);
if (info.collider.tag == "Player")
break;
temp = Vector3.Lerp(temp, player.position + up, 3 * Time.deltaTime);
}
Vector3 temp2 = Vector3.Lerp(transform.position, temp, Time.deltaTime * 3);
transform.position = temp2;
transform.LookAt(player);
}