Hey all. This code for the camera is close to finished, but I have one important thing left that I can’t figure out.
Currently, I have an object that the camera rotates around. pressing Q or E rotates the camera 90 degrees around the target, one for each direction. Each rotation takes about 20 frames.
When the button is pressed, an animation is called from the animator to bring it into one of two states, clockwise rotation or counterclockwise rotation. I have an event on the last frame of each animation that causes the isSpinning bool to become false, stopping the animation.
The problem here is that I want the player to be able to press the rotation button WHILE it’s rotating already, in order to queue up another rotation immediately after the first one ends. So in that way, the player could double-tap to swing the camera in a 180. There would be a slight pause since it’s two easing animations, but it still would feel a lot better to be able to control it like that than how it’s controlled right now.
Right now, the only time you can rotate the camera is when the camera is not rotating at all. As soon as the animation has stopped, you can start up another animation, but I’m trying to figure out a way it can “keep track” of how many extra times the user inputs the rotation button, and then allow it to rotate extra times accordingly.
I’ve tried several different ways so far and I don’t know how I can achieve this. Any help you might be willing to offer would be very appreciated, thank you.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using UnityEditor.VersionControl;
using UnityEngine;
using UnityEngine.InputSystem;
public class CameraRotate : MonoBehaviour
{
Animator anim;
public InputMaster controls;
private bool isSpinning = false;
public float camDirection = 0;
void Awake()
{
controls = new InputMaster();
controls.PlayerN.Camera.performed += ctx => Rotation(ctx.ReadValue<float>());
anim = GetComponent<Animator>();
}
IEnumerator WaitForRotationCW()
{
anim.SetBool("camSpinCW", true);
isSpinning = true;
camDirection -= 1;
yield return null;
anim.SetBool("camSpinCW", false);
}
IEnumerator WaitForRotationCCW()
{
anim.SetBool("camSpinCCW", true);
isSpinning = true;
camDirection += 1;
yield return null;
anim.SetBool("camSpinCCW", false);
}
void RotationAnimationEnded()
//this gets called up when a camera rotation animation reaches it's final frame,
//where I have an animation event set up.
{
isSpinning = false;
}
void Rotation(float amount)
{
Debug.Log("rotation number is " + amount);
float holdingRotate = controls.PlayerN.Camera.ReadValue<float>();
if (holdingRotate == 1 && isSpinning == false)
{
StartCoroutine(WaitForRotationCW());
}
else if (holdingRotate == -1 && isSpinning == false)
{
StartCoroutine(WaitForRotationCCW());
}
}
void Update()
{
Debug.Log("spinning: " + isSpinning);
float holdingRotate = controls.PlayerN.Camera.ReadValue<float>();
Debug.Log("holding rotate is " + holdingRotate);
switch (camDirection)
{
case -3:
case 1:
Debug.Log("Facing East");
break;
case -2:
case 2:
Debug.Log("Facing South");
break;
case -1:
case 3:
Debug.Log("Facing West");
break;
case 0:
Debug.Log("Facing North");
break;
}
if (camDirection >= 4 || camDirection <= -4)
{
camDirection = 0;
}
}
private void OnEnable()
{
controls.Enable();
}
private void OnDisable()
{
controls.Disable();
}
}