- Home /
Question by
grecianav39 · Jun 04, 2020 at 03:38 PM ·
camera2d game2d-platformercamera-movement2d platformer
Cinemachine camera shake on button press
Hello! I was using a camera script on the main camera that made the camera shake whenever I pressed x (This makes my character dash). I switched to Cinemachine in order to move the camera around to certain areas. Now I am not sure on how to implement the camera shake when X is pressed while using Cinemachine. Here is what the script on my camera looked like:
void Start()
{
playerTransform = GameObject.FindGameObjectWithTag("Player").transform;
}
// Update is called once per frame
void Update()
{
hasDashed = character.hasDashed;
Vector3 temp = transform.position;
temp.x = playerTransform.position.x+xPos;
temp.y = playerTransform.position.y+yPos;
transform.position = temp;
transform.position = new Vector3(Mathf.Clamp(transform.position.x, leftLim, rightLim), Mathf.Clamp(transform.position.y, botLim, topLim), transform.position.z);
if (Input.GetKeyDown(KeyCode.X) && hasDashed)
{
Debug.Log(hasDashed);
StartShake(shakeTime, shakeIntensity);
}
Debug.Log(hasDashed);
}
private void LateUpdate()
{
if(shakeTimeRem > 0)
{
shakeTimeRem -= Time.deltaTime;
float x = Random.Range(-1f, 1f) * shakeIntensity;
float y = Random.Range(-1f, 1f) * shakeIntensity;
transform.position += new Vector3(x, y, 0f);
}
}
void StartShake(float length, float power)
{
shakeTimeRem = length;
}
Any suggestions? Thanks :D
Comment