Camera looking at the player at an angle
So I am working on a camera controller that's pretty similar to the ones found in newer 3d Zelda games following this tutorial series: https://www.youtube.com/playlist?list=PLKFvhfT4QOqlEReJ2lSZJk_APVq5sxZ-x But it's pretty old, I'm currently on tutorial #12, and that's when i hit my problem. When I try to move my player forward/backward the camera looks at the player at an angle (30 degrees give or take) and quite frankly, I have no idea how to fix it. Here's the code snippet:
[SerializeField]
private float distAway;
[SerializeField]
private float distUp;
[SerializeField]
private Transform followXFrom;
[SerializeField]
private float lookDirectDampTime = 0.1f;
private Vector3 velocityLookDirect = Vector3.zero;
private Vector3 curLookDirect;
private Vector3 targPos;
private Vector3 lookDirect;
followXFrom = GameObject.FindWithTag("Player").transform;
lookDirect = followXFrom.forward;
curLookDirect = followXFrom.forward;
case CamStates.Behind:
ResetCamera();
if (playerLogic.Speed > playerLogic.LocomotionThreshold && playerLogic.IsInLocomotion())
{
lookDirect = Vector3.Lerp(followXFrom.right * (leftX < 0 ? 1f : -1f), followXFrom.forward * (leftY < 0 ? -1f : 1f), Mathf.Abs(Vector3.Dot(this.transform.forward, followXFrom.forward)));
curLookDirect = Vector3.Normalize(characterOffset - this.transform.position);
curLookDirect.y = 0;
curLookDirect = Vector3.SmoothDamp(curLookDirect, lookDirect, ref velocityLookDirect, lookDirectDampTime);
}
break;
private void ResetCamera()
{
transform.localRotation = Quaternion.Lerp(transform.localRotation, Quaternion.identity, Time.deltaTime);
}
As usual, Any help is greatly appreciated
Answer by AedanCGraves · Jan 02, 2017 at 01:13 AM
Fixed it! there are probably better ways to do it, but i just modified an if state meant a bit. Here's the changes:
[SerializeField]
private float distAway;
[SerializeField]
private float distUp;
[SerializeField]
private Transform followXFrom;
[SerializeField]
private float lookDirectDampTime = 0.1f;
private Vector3 velocityLookDirect = Vector3.zero;
private Vector3 curLookDirect;
private Vector3 targPos;
private Vector3 lookDirect;
followXFrom = GameObject.FindWithTag("Player").transform;
lookDirect = followXFrom.forward;
curLookDirect = followXFrom.forward;
void LateUpdate() { float leftX = Input.GetAxis("FPSmodeH"); %|159717598_18|% %|979168935_19|% }
case CamStates.Behind:
ResetCamera();
if (playerLogic.Speed > playerLogic.LocomotionThreshold && playerLogic.IsInLocomotion() && (leftX > 0.15f || leftX < -0.15f))
{
lookDirect = Vector3.Lerp(followXFrom.right * (leftX < 0 ? 1f : -1f), followXFrom.forward * (leftY < 0 ? -1f : 1f), Mathf.Abs(Vector3.Dot(this.transform.forward, followXFrom.forward)));
curLookDirect = Vector3.Normalize(characterOffset - this.transform.position);
curLookDirect.y = 0;
curLookDirect = Vector3.SmoothDamp(curLookDirect, lookDirect, ref velocityLookDirect, lookDirectDampTime);
targPos = characterOffset + followXFrom.up * distUp - Vector3.Normalize(curLookDirect) * distAway;
}
else {targPos = characterOffset + followXFrom.up * distUp - followXFrom.forward * distAway; }
break;
private void ResetCamera()
{
transform.localRotation = Quaternion.Lerp(transform.localRotation, Quaternion.identity, Time.deltaTime);
}
PS. Just realized that there are a few undefined floats and stuff in the code I gave you :P their highlighted in red... well some of them...