PHP群:95885625 Hbuilder+MUI群:81989597 站长QQ:634381967
    您现在的位置: 首页 > 开发编程 > Unity3D教程 > 正文

    Unity 相机跟随的几种方法

    作者:张志勇来源:网络浏览:时间:2020-09-30 00:07:50我要评论
    导读:一public class FollowP : MonoBehaviour{//玩家Public Transform p ; public float speed=4.0f; void Start () {//初始...

    public class FollowP : MonoBehaviour{

    //玩家

    public  Transform p ;    

    public float speed=4.0f;    

    void Start ()    {

    //初始化 或者 拖物体

     }    

    void Update ()    {

    //摄像机跟随

    Vector3 targetPos =p .position+ new Vector3 (0,2.0f, -2.7f);

    transform.position= Vector3.Slerp (transform.position, targetPos,speed*Time.deltaTime);        

    Quaternion targetRotation = Quaternion.LookRotation (p .position- transform.position);       

     transform.rotation = Quaternion.Slerp (transform.rotation, targetRotation,speed*Time.deltaTime);   

     }

    }

     

    public class Camera : MonoBehaviour{

    //摄像机跟离角色有多远

    public float distanceAway;

    //摄像机在角色上方多高

    public float distanceUp;

    //摄像机移动的速度

    public float smooth;

    //目标位置

    private Vector3 targetPosition;

    //摄像机所跟随的对象的位置,此处是角色物体的位置

    Transform follow;

    // Use this for initialization

    void Start ()        {

    //得到要跟随的物体对象的位置

    follow = Game .FindGame WithTag ("P ").transform;       

     }

    // Update is called once per

    void Update ()        {

    //得到摄像机要移动的目标位置

    targetPosition = follow.position+ Vector3.up * distanceUp -follow.forward * distanceAway;

    //摄像机从当前位置移动到目标位置

    transform.position= Vector3.Lerp (transform.position, targetPosition, Time.deltaTime * smooth);

    //摄像机要看向角色物体

    transform.LookAt (follow);        

    }

    }

    摄像机随鼠标旋转并且限制角度

    public class Cube : MonoBehaviour{floatver;   

     // 限制角度

    float fv=20;    

    Quaternion last;    

     void Update ()    {          

    ver = Input.GetAxis ("Mouse Y");

    float angle = Vector3.Angle (Vector3.up,transform.forward);

    if(angle >90+fv|| angle <90-fv) {

    transform.rotation =last;         

     }

    else{

    last = transform.rotation;             

     Vector3 tmp = newVector3 (transform.eulerAngles.x - ver * Time.deltaTime * 60f,transform.eulerAngles.y,transform.eulerAngles.z);

    transform.eulerAngles = tmp;          

    }    

     }}
    转载请注明(B5教程网)原文链接:https://b5.mxunkeji.com/content-130-5976-1.html
    相关热词搜索: