alayeshi的专栏

Unity的世界我曾来过,程序的世界很单纯!

unity3d相机跟随物体平滑移动(C#代码)
using UnityEngine;
using System.Collections;

public class CameraFollow : MonoBehaviour {
    
        public Transform target;
        private Vector3 wantedPosition;
    
        private float currentX;
        private float currentY;
        private float currentZ;
    
        private float xVelocity = 0.0F;
        private float yVelocity = 0.0F;
        private float zVelocity = 0.0f;
        private float distanceSnapTime = 0.1f;
    
    // Update is called once per frame
    void Update () {
    
            Vector3 targetPos = target.position;
            
            wantedPosition.x =  targetPos.x;
            
            wantedPosition.z = targetPos.z - 5f;//Vector3.forward*distance;   
            
            wantedPosition.y = targetPos.y -2f;// + heightAbovePlayer;
            
            currentX = Mathf.SmoothDamp(currentX, wantedPosition.x, ref xVelocity, distanceSnapTime);
            
            currentY = Mathf.SmoothDamp(currentY, wantedPosition.y, ref yVelocity, distanceSnapTime);
            
            currentZ = Mathf.SmoothDamp(currentZ, wantedPosition.z, ref zVelocity, 0.5f);
            
            transform.position = new Vector3(currentX,currentY,currentZ);
            transform.LookAt(transform.position + new Vector3(0f,0.95f,1));
    }
}
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/alayeshi/article/details/46804371
个人分类: unity3D
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

unity3d相机跟随物体平滑移动(C#代码)

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭