Unity 3D游戏斜俯视角相关脚本

键盘控制的旋转:

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);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值