Hello everyone !
I am having some troubles figuring out what is going on with my third person camera system.
It is quite unusual since the system I am looking to put in place is to make a transition from different state of the camera, and overall it works, however I am still getting that jitter from happening when moving around when my character is aiming.
I am using a character controller so I am doing all the movement in FixedUpdate, as well as the rotation but I also tried in LateUpdate and Update, and I am starting to think that the jitter is not happening because of that, but because of the logic of the system, possibly an internal cycle in it, and I would like to know your opinion about it.
How it works : My character is moving around and camera is following it and can rotate around it.
I have a ‘ghostCam’ that is at the same place as the maincamera except on the Y axis.
When I am aiming ( using mouse button 1 ) I am entering in aiming state, the main camera is parented to the inside of the player so it gets rotated as the player is, and the ‘player’ is facing now the direction we are facing with the camera. To align the player correctly I am using the ‘ghostCam’ entity to make a lookRotation Quaternion operation, and orientating the player using the position of the ghostCam, being adjusted with the mouse inputs.
Is this logic ok to apply ?
this is what it is causing :
Here is a link to the scene in case someone would want to have a look : Dropbox
This is the playerScript:
// Update is called once per frame
void Update()
{
horizontalInput = Input.GetAxisRaw("Horizontal");
verticalInput = Input.GetAxisRaw("Vertical");
if (moveState == e_movementState.idle)
{
return;
}
else if (moveState == e_movementState.walking)
{
CheckForAiming();
}
if (lookState == e_lookState.aiming)
{
Quaternion newRotation = Quaternion.LookRotation(transform.position - cameraGhostFree.position, Vector3.up);
transform.rotation = newRotation;
playerModel.transform.rotation = newRotation;
}
else
{
if (horizontalInput != 0 || verticalInput != 0)
{
Quaternion test = Quaternion.LookRotation(transform.position - cameraGhostFree.position);
transform.rotation = test;
Quaternion newRotation = Quaternion.LookRotation(new Vector3(moveDirection.x, 0f, moveDirection.z));
playerModel.transform.rotation = Quaternion.Slerp(playerModel.transform.rotation, newRotation, rotateSpeed * Time.deltaTime);
}
}
}
private void FixedUpdate()
{
DoMove();
}
void CheckForAiming()
{
if (Input.GetMouseButton(1))
{
lookState = e_lookState.aiming;
}
else
{
lookState = e_lookState.free;
}
}
The Camera script :
void Update()
{
angle += (-Input.GetAxisRaw("Mouse X")) * 0.1f;
angleY += Input.GetAxisRaw("Mouse Y") * 0.1f;
}
// Update is called once per frame
void LateUpdate()
{
if (Input.GetMouseButton(1))
{
transform.parent = cameraPivot;
}
else
{
transform.parent = null;
angleY = Mathf.Clamp(angleY, -1f, 1f);
Vector3 center = player.position;
position.x = center.x + 10f * Mathf.Cos(angle);
position.z = center.z + 10f * Mathf.Sin(angle);
position.y = center.y + 10f * Mathf.Sin(-angleY);
if (!forceCameraPosInit)
{
angleY = -0.345f;
forceCameraPosInit = true;
}
Vector3 velocity = Vector3.zero;
Vector3 smoothMov = Vector3.SmoothDamp(transform.position, position, ref velocity, 0.01f);
transform.position = smoothMov;
Quaternion lookRot = Quaternion.LookRotation(player.position - transform.position);
transform.rotation = lookRot;
}
}
And the CamGhost script :
// Update is called once per frame
void Update()
{
angle += (-Input.GetAxisRaw("Mouse X")) * 0.1f;
angleY += Input.GetAxisRaw("Mouse Y") * 0.1f;
if (Input.GetMouseButton(1))
{
position.y = playerPosition.position.y;
Vector3 center = playerPosition.position;
position.x = center.x + 10f * Mathf.Cos(angle);
position.z = center.z + 10f * Mathf.Sin(angle);
transform.position = position;
}
else
{
position.y = playerPosition.position.y;
Vector3 center = playerPosition.position;
position.x = center.x + 10f * Mathf.Cos(angle);
position.z = center.z + 10f * Mathf.Sin(angle);
transform.LookAt(playerPosition.position, Vector3.up);
transform.position = position;
}
}
Thanks a lot guys !