當(dāng)前位置:
首頁 >
Unity 一个简单的鼠标跟随
發(fā)布時間:2023/12/29
30
生活家
生活随笔
收集整理的這篇文章主要介紹了
Unity 一个简单的鼠标跟随
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
這份代碼主要是根據(jù)鼠標(biāo)在屏幕上的移動來操作攝像機(jī)
using UnityEngine;
using System.Collections;
public class LookAt:MonoBehaviour{
public Transform target;//攝像機(jī)跟隨目標(biāo)
public float distance;//攝像機(jī)跟隨距離
public Vector2 mouseAngles;//鼠標(biāo)在屏幕上的位置
public float yrotationMin;
public float yrotationMax;
public float xspeed;
public float yspeed;
void Start(){
mouseAngles.x=this.target.eulerAngles.x;
mouseAngles.y=this.target.eulerAngles.y;
distance=3;
xspeed=200;
yspeed=100;
yrotationMax=60;
yrotationMin=-30;
}
void LateUpdate(){
if(target){
mouseAngles.x+=Input.GetAxis("Mouse X")*xspeed*0.02f;
mouseAngles.y+=Input.GetAxis("Mouse Y")*yspeed*0.02f;
mouseAngles.y=transformAngle(mouseAngles.y,yrotationMin,yroataionMax);
Quaternion rotation=Quaternion.Euler(mouseAngles.y,mouseAngles.x,0);
//下面代碼的作用為 使攝像機(jī)朝向目標(biāo) 并在目標(biāo)后方distance米處
Vector3 position=rotation*new Vector3(0.0f,0.0f,-distance)+target.position;
this.transform.rotation=rotation;
this.transform.position=position;
}
}
float transformAngle(float angle,float min,float max){
if(angle<-360)angle+=360;
if(angle>360)angle-=360;
return Mathf.Clamp(angle,min,max);
}
}
總結(jié)
以上是生活随笔為你收集整理的Unity 一个简单的鼠标跟随的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在网页中发起QQ临时对话的方法
- 下一篇: linux文件分割(将大的日志文件分割成