Unity3D의 ThirdPersonCamera.js 스크립트를 기반으로 한 사용자 정의 하향식 카메라 로직 스크립트를 작성했습니다. 모든 것이 제대로 작동하는 것처럼 보입니다. 카메라는 XZ 평면에서 대상 플레이어를 따라 가고 플레이어가 점프 할 때 Y 축을 따라 이동합니다.Unity3D - Top Down 카메라 로직이 Transform.LookAt 사용시 잠김.
카메라 만 플레이어를보고 있지 않습니다. 그래서 카메라에서 Transform.LookAt()를 사용하여 카메라를 카메라에서 직접 내려다 보았습니다. 이로 인해 카메라가 플레이어에서 올바르게 보이지 않게되지만 WASD를 통한 이동은 더 이상 작동하지 않습니다. 플레이어가 거기 앉아 있습니다. 점프를 위해 스페이스 바를 사용하면 여전히 작동합니다.
카메라의 변형 방향이 플레이어 개체의 움직임에 어떤 영향을 주어야합니까?
내 스크립트 코드는 다음과 같습니다 : 나는 문제 코드 주석이있는 문제의 코드 섹션을 표시 한
// The transform of the camera that we're manipulating
var cameraTransform : Transform;
// The postion that the camera is currently focused on
var focusPosition = Vector3.zero;
// The idle height we are aiming to be above the target when the target isn't moving
var idleHeight = 7.0;
// How long should it take the camera to focus on the target on the XZ plane
var xzSmoothLag = 0.3;
// How long should it take the camera to focus on the target vertically
var heightSmoothLag = 0.3;
private var _target : Transform;
private var _controller : ThirdPersonController;
private var _centerOffset = Vector3.zero;
private var _headOffset = Vector3.zero;
private var _footOffset = Vector3.zero;
private var _xzVelocity = 0.0;
private var _yVelocity = 0.0;
private var _cameraHeightVelocity = 0.0;
// ===== UTILITY FUNCTIONS =====
// Apply the camera logic to the camera with respect for the target
function process()
{
// Early out if we don't have a target
if (!_controller)
return;
var targetCenter = _target.position + _centerOffset;
var targetHead = _target.position + _headOffset;
var targetFoot = _target.position + _footOffset;
// Determine the XZ offset of the focus position from the target foot
var xzOffset = Vector2(focusPosition.x, focusPosition.z) - Vector2(targetFoot.x, targetFoot.z);
// Determine the distance of the XZ offset
var xzDistance = xzOffset.magnitude;
// Determine the Y distance of the focus position from the target foot
var yDistance = focusPosition.y - targetFoot.y;
// Damp the XZ distance
xzDistance = Mathf.SmoothDamp(xzDistance, 0.0, _xzVelocity, xzSmoothLag);
// Damp the XZ offset
xzOffset *= xzDistance;
// Damp the Y distance
yDistance = Mathf.SmoothDamp(yDistance, 0.0, _yVelocity, heightSmoothLag);
// Reposition the focus position by the dampened distances
focusPosition.x = targetFoot.x + xzOffset.x;
focusPosition.y = targetFoot.y + yDistance;
focusPosition.z = targetFoot.z + xzOffset.y;
var minCameraHeight = targetHead.y;
var targetCameraHeight = minCameraHeight + idleHeight;
// Determine the current camera height with respect to the minimum camera height
var currentCameraHeight = Mathf.Max(cameraTransform.position.y, minCameraHeight);
// Damp the camera height
currentCameraHeight = Mathf.SmoothDamp(currentCameraHeight, targetCameraHeight, _cameraHeightVelocity, heightSmoothLag);
// Position the camera over the focus position
cameraTransform.position = focusPosition;
cameraTransform.position.y = currentCameraHeight;
// PROBLEM CODE - BEGIN
// Have the camera look at the focus position
cameraTransform.LookAt(focusPosition, Vector3.forward);
// PROBLEM CODE - END
Debug.Log("Camera Focus Position: " + focusPosition);
Debug.Log("Camera Transform Position: " + cameraTransform.position);
}
// ===== END UTILITY FUNCTIONS =====
// ===== UNITY FUNCTIONS =====
// Initialize the script
function Awake()
{
// If the camera transform is unassigned and we have a main camera,
// set the camera transform to the main camera's transform
if (!cameraTransform && Camera.main)
cameraTransform = Camera.main.transform;
// If we don't have a camera transform, report an error
if (!cameraTransform)
{
Debug.Log("Please assign a camera to the TopDownThirdPersonCamera script.");
enabled = false;
}
// Set the target to the game object transform
_target = transform;
// If we have a target set the controller to the target's third person controller
if (_target)
{
_controller = _target.GetComponent(ThirdPersonController);
}
// If we have a controller, calculate the center offset and head offset
if (_controller)
{
var characterController : CharacterController = _target.collider;
_centerOffset = characterController.bounds.center - _target.position;
_headOffset = _centerOffset;
_headOffset.y = characterController.bounds.max.y - _target.position.y;
_footOffset = _centerOffset;
_footOffset.y = characterController.bounds.min.y - _target.position.y;
}
// If we don't have a controller, report an error
else
Debug.Log("Please assign a target to the camera that has a ThirdPersonController script attached.");
// Apply the camera logic to the camera
process();
}
function LateUpdate()
{
// Apply the camera logic to the camera
process();
}
// ===== END UNITY FUNCTIONS =====
. 문제 코드가 제거되면 WASD 이동이 다시 작동하지만 카메라가 더 이상 대상을보고 있지 않습니다.
이 문제에 대한 통찰력을 얻으실 수 있습니다.