- 最后登录
- 2018-6-4
- 注册时间
- 2012-12-15
- 阅读权限
- 90
- 积分
- 52052
- 纳金币
- 25
- 精华
- 1
|
unity3d脚本:摄像机穿墙时自动缩小距离
class SoldierCamera : MonoBehaviour { private Transform target; private Transform camTransform; public LayerMask hitLayer; private Vector3 cPos; public float normalDistance; private float targetDistance; private Vector3 camDir;
void Start () { }
void LateUpdate () { CameraMovement(); }
void CameraMovement() { camDir = (camTransform.position - target.position).normalized; cPos = target.position; targetDistance = Vector3.Distance(camTransform.position,target.position);
RaycastHit hit; if (Physics.Raycast(cPos, camDir, out hit, targetDistance + 0.2f, hitLayer)) { float t = hit.distance - 0.1f; camTransform.position = t * camDir + cPos; } else { if (targetDistance != normalDistance) { float t = Mathf.Lerp(targetDistance,normalDistance,Time.deltaTime * 15); camTransform.position = t * camDir + cPos; } } }
}
|
|