For my 3d game, I have attached a camera to follow my player object in position and rotation change.
But overall I was getting jerky camera follow behavior. I double-checked by stopping the camera follow with player movement behavior. If I don’t attach camera to a player then the player has smooth movement and rotation but as I started to follow the player, the screen is started stuttering.
Here is the code that I have used to follow the player object in rotation and position:
public class CameraFollow : MonoBehaviour
{
Vector3 offset;
Quaternion offsetRotation;
//
public Transform target;
[SerializeField] float damping = 1;
void Start()
{
offset = transform.position - target.position;
offsetRotation = transform.rotation;
}
void LateUpdate()
{
if (!target)
return;
float currentAngle = transform.eulerAngles.y;
float desiredAngle = target.eulerAngles.y;
float angle = Mathf.LerpAngle(currentAngle, desiredAngle, Time.deltaTime * damping);
Quaternion rotation = Quaternion.Euler(0f, angle, 0f);
transform.position = target.position + (rotation * offset);
transform.LookAt(target);
transform.rotation *= Quaternion.Euler(-10f, 0f, 0f);
}
}
As per my thinking, transform.LookAt statement creating a problem in smooth motion. Please share your suggestion to make overall feel smooth.
siddharth3322:
For my 3d game, I have attached a camera to follow my player object in position and rotation change.
But overall I was getting jerky camera follow behavior. I double-checked by stopping the camera follow with player movement behavior. If I don’t attach camera to a player then the player has smooth movement and rotation but as I started to follow the player, the screen is started stuttering.
Here is the code that I have used to follow the player object in rotation and position:
public class CameraFollow : MonoBehaviour
{
Vector3 offset;
Quaternion offsetRotation;
//
public Transform target;
[SerializeField] float damping = 1;
void Start()
{
offset = transform.position - target.position;
offsetRotation = transform.rotation;
}
void LateUpdate()
{
if (!target)
return;
float currentAngle = transform.eulerAngles.y;
float desiredAngle = target.eulerAngles.y;
float angle = Mathf.LerpAngle(currentAngle, desiredAngle, Time.deltaTime * damping);
Quaternion rotation = Quaternion.Euler(0f, angle, 0f);
transform.position = target.position + (rotation * offset);
transform.LookAt(target);
transform.rotation *= Quaternion.Euler(-10f, 0f, 0f);
}
}
As per my thinking, transform.LookAt statement creating a problem in smooth motion. Please share your suggestion to make overall feel smooth.
Why not make the camera child of the player ? It will follow the player automatic.
Do you think this is an optimal solution?
Then why there are so many ways exist for the camera to follow scripts!!
Even Unity within their demo projects used camera follow script rather than making its child.
adi7b9
January 2, 2020, 12:46pm
4
Player movement is on Update?
I was taking input from the Update method and doing actual player movement in FixedUpdate using this way.
private void FixedUpdate()
{
playerRotation.y = horizontalMovement;
playerMovement.z = forwardMovement;
myRigidbody.MovePosition(myRigidbody.position + transform.TransformDirection(playerMovement) * movementSpeed * Time.fixedDeltaTime);
Quaternion deltaRotation = Quaternion.Euler(playerRotation * rotationSpeed * Time.fixedDeltaTime);
myRigidbody.MoveRotation(myRigidbody.rotation * deltaRotation);
}
adi7b9
January 3, 2020, 8:30am
6
Hmm then try to use this
public float smoothSpeed = 0.2f;
//in LateUpdate
Vector3 dest = target.position + (rotation * offset);
transform.position = Vector3.Lerp(transform.position, dest, smoothSpeed);
Also, I have tested that if I stop camera follow then player doing movement and rotation properly it does not have any jittery movement.
So that I am concerning about Camera Follow related issue.
But let me try your given suggestion and apply.
I tested this and it gave little bit better output compare to before but rotation come too slow
If I increase smoothness value from 0.2 to 0.5 then the same problem like before started.
adi7b9
January 3, 2020, 9:39am
9
1 Like