Question by
edriclzw · Apr 17, 2018 at 03:44 PM ·
cameracamera-movementupdate functionshake
Camera Shake not working due to LateUpdate
So I'm using a LateUpdate for my camera follow function, and an Update for the camera shake function.
If I were to change the camera function to a FixedUpdate, the camera shake will work, however the camera will be jittery when my character moves at a certain speed., which I found using the LateUpdate function fixed that.
I don't know what is wrong and I can't seem to work around it, can anybody please help me? I want the camera to be smooth while still having the camera shake to happen.
This is the code that I am using;
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class cameraFollow : MonoBehaviour {
public Transform target;
public Vector3 offset;
public bool bounds;
public Vector3 minCamPos;
public Vector3 maxCamPos;
public float shakeTimer;
public float shakeAmount;
void Update ()
{
if (shakeTimer >= 0)
{
Vector2 ShakePos = Random.insideUnitCircle * shakeAmount;
transform.position = new Vector3 (transform.position.x + ShakePos.x, transform.position.y + ShakePos.y, transform.position.z);
shakeTimer -= Time.deltaTime;
}
}
void LateUpdate ()
{
transform.position = target.position + offset;
if (bounds)
{
transform.position = new Vector3 (Mathf.Clamp (transform.position.x, minCamPos.x, maxCamPos.x),
Mathf.Clamp (transform.position.y, minCamPos.y, maxCamPos.y),
Mathf.Clamp (transform.position.z, minCamPos.z, maxCamPos.z));
}
}
public void ShakeCamera (float shakePwr, float shakeDur)
{
shakeAmount = shakePwr;
shakeTimer = shakeDur;
}
}
Comment