Hi,
I’m trying to make a cannon fire a ball. However, transform.rotation in the “Aim” event is correct, while transform.rotation in the “Fire” event is the empty quaternion. The only difference between the “Aim” and “Fire” events is that “Aim” is a Value / Axis event and “Fire” is a button event.
My code:
private float angleRate; // Degrees per frame.
// Update is called once per frame
void Update()
{
transform.Rotate(0, 0, angleRate);
}
// Rotate the cannon.
public void Aim(InputAction.CallbackContext context)
{
Vector3 axis = new Vector3(-1, -1, -1);
float angle = -1;
// Aim the cannon.
if (context.started)
{
angleRate = context.ReadValue<float>();
transform.rotation.ToAngleAxis(out angle, out axis);
Debug.Log("Aim! " + angle + " @ " + axis + " {" + transform.rotation + "}");
} else if (context.canceled)
{
angleRate = 0f;
transform.rotation.ToAngleAxis(out angle, out axis);
Debug.Log("Stop! " + angle + " @ " + axis + " {" + transform.rotation + "}");
}
}
public void Fire(InputAction.CallbackContext context)
{
Vector3 axis = new Vector3(-1, -1, -1);
float angle = -1;
transform.rotation.ToAngleAxis(out angle, out axis);
Debug.Log("Fire! " + angle + " @ " + axis + " {" + transform.rotation + "}");`
}
Sample output:
Aim! 0 @ (1.0, 0.0, 0.0) {(0.0, 0.0, 0.0, 1.0)}
Stop! 24.89999 @ (0.0, 0.0, -1.0) {(0.0, 0.0, -0.2, 1.0)}
Fire! 0 @ (1.0, 0.0, 0.0) {(0.0, 0.0, 0.0, 1.0)}
(the output for “Fire” does not change for context.performed and context.canceled".)
Everything I’ve tried hasn’t worked; any help is appreciated. Thanks!