您的位置:首页 > 移动开发 > Unity3D

unity中实现的第三人称摄像机控制脚本。

2016-01-22 12:21 459 查看
  using UnityEngine;

 [RequireComponent( typeof( Camera ) )]

 public class ThirdPersonCamera : MonoBehaviour

 {

     

     public Transform    follow;

     public     float     m_fDistance = 7.0f;

     public     float     m_fXSpeed = 350.0f;

     public     float     m_fYMinLimit= 0.1f;

     public     float     m_fYMaxLimit = 89.9f;

     private    float     m_fXRot = 0.0f;

     public     float     m_fYRot = 0.0f;

     public float
 speed = 5.0f;

     

     void Start()

     {

     follow=follow?follow:GameObject.Find("Player").transform;

     }

     

     void LateUpdate(){

         if(Input.GetMouseButton(0)){

             m_fXRot += Input.GetAxis("Mouse X")/10.0f * m_fXSpeed;

             m_fYRot -= Input.GetAxis("Mouse Y")/10.0f * m_fXSpeed; 

         }

         m_fYRot = Mathf.Clamp(m_fYRot,m_fYMinLimit, m_fYMaxLimit);

        Vector3 negDistance= new Vector3(0.0f, 0.0f, -m_fDistance);

        //transform.rotation = Quaternion.Lerp(transform.rotation,Quaternion.Euler(m_fYRot, m_fXRot, 0),Time.deltaTime*speed);

        transform.rotation =Quaternion.Euler(m_fYRot, m_fXRot, 0);

        transform.position = transform.rotation * negDistance + follow.position;

    }

 }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: