Unity3d模仿龙之谷的Camare

用法:先在你的Player里新建一个空的Object重命名为CameraTarget(你起什么名字都行),然后将脚本拖动到你的Player里,将你新建的 CameraTarget 拖动到脚本的Target属性,运行你就可以看到效果了


using UnityEngine;
using System.Collections;
 
public class DragonHillCamera : MonoBehaviour
{
    public bool HideAndShowCursor = true;
    public bool LockRotationWhenRightClick = false;
    public bool UseBlurEffect = true;
    public bool UseFogEffect = true;
    public Transform target;
    
    public float targetHeight = 1.0f;
    public float distance = 5.0f;
 
    public float maxDistance = 20;
    public float minDistance = .6f;
 
    public float xSpeed = 250.0f;
    public float ySpeed = 120.0f;
 
    public int yMinLimit = -80;
    public int yMaxLimit = 80;
 
    public int zoomRate = 40;
 
    public float rotationDampening = 3.0f;
    public float zoomDampening = 10.0f;
 
    private float x = 0.0f;
    private float y = 0.0f;
    private float currentDistance;
    private float desiredDistance;
    private float correctedDistance;
    private bool grounded = false;
 
    void Start ()
    {
        Screen.lockCursor = true;
        Vector3 angles = transform.eulerAngles;
        x = angles.x;
        y = angles.y;
 
        currentDistance = distance;
        desiredDistance = distance;
        correctedDistance = distance - 0.2f;
 
        // Make the rigid body not change rotation
        if (rigidbody)
            rigidbody.freezeRotation = true;
    }
 
    void LateUpdate ()
    {
        // Don't do anything if target is not defined
        if (!target){
            GameObject go = GameObject.Find("Player");
            target = go.transform;
            transform.LookAt(target);
            return;
        }
        // If either mouse buttons are down, let the mouse govern camera position
        if(LockRotationWhenRightClick== false){
                x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
                y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
            }
        if (Input.GetMouseButton(0)){
            if(LockRotationWhenRightClick== false){
                x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
                y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
            }
        }
        y = ClampAngle(y, yMinLimit, yMaxLimit);
 
        // set camera rotation
        Quaternion rotation = Quaternion.Euler(y, x, 0);
 
        // calculate the desired distance
        desiredDistance -= Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime * zoomRate * Mathf.Abs(desiredDistance);
        desiredDistance = Mathf.Clamp(desiredDistance, minDistance, maxDistance);
        correctedDistance = desiredDistance;
 
        // calculate desired camera position
        Vector3 position = target.position - (rotation * Vector3.forward * desiredDistance + new Vector3(0, -targetHeight, 0));
 
        // check for collision using the true target's desired registration point as set by user using height
        RaycastHit collisionHit;
        Vector3 trueTargetPosition = new Vector3(target.position.x, target.position.y + targetHeight, target.position.z);
 
        // if there was a collision, correct the camera position and calculate the corrected distance
        bool isCorrected = false;
        if (Physics.Linecast(trueTargetPosition, position, out collisionHit))
        {
            if(collisionHit.transform.name!=target.name){
                position = collisionHit.point;
                correctedDistance = Vector3.Distance(trueTargetPosition, position);
                isCorrected = true;
            }
        }
         
        // For smoothing, lerp distance only if either distance wasn't corrected, or correctedDistance is more than currentDistance
        currentDistance = !isCorrected || correctedDistance > currentDistance ? Mathf.Lerp(currentDistance, correctedDistance, Time.deltaTime * zoomDampening) : correctedDistance;
 
        // recalculate position based on the new currentDistance
        position = target.position - (rotation * Vector3.forward * currentDistance + new Vector3(0, -targetHeight - 0.05f, 0));
 
        transform.rotation = rotation;
        transform.position = position;
         
    }
 
    private static float ClampAngle(float angle, float min, float max)
    {
        if (angle < -360)
            angle += 360;
        if (angle > 360)
            angle -= 360;
        return Mathf.Clamp(angle, min, max);
    }
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值