Hello all.
if (Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.W))
weapon.rotation = Quaternion.Euler(0, 0, 90 * transform.localScale.x);
else if (Input.GetKey(KeyCode.UpArrow) && Input.GetKey(KeyCode.RightArrow))
weapon.rotation = Quaternion.Euler(0, 0, 45 * transform.localScale.x);
else if (Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.D))
weapon.rotation = Quaternion.Euler(0, 0, 45 * transform.localScale.x);
In my code i try to shoot 90 degrees upwards when pressing UpArrow or W and 45 degrees upwards when pressing UpArrow AND RIghtArrow, or W AND D. But it doesn’t work. Firing code works. Any help? Thanks in advance.
You need to use code tags and, probably, try to specify your angles in radians instead of degrees?
Thing is it works for shooting at 0 and 90 degrees, but when it comes to Input.GetKey && Input.GetKey combination it just shoots again at 0 or 90 degrees and not 45.
This condition will be true if one of keys is pressed or both keys are pressed. Moreover, if first key is pressed, second will not be checked. Check docs for logic operators OR, AND, XOR. https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/boolean-logical-operators
Just chance the order of your if statements, your current first if should be the last one
You could just create a directional vector and rotate to that;
private Vector3
_fwd = Vector3.forward,
_back = Vector3.back,
_left = Vector3.left,
_right = Vector3.right,
_zero = Vector3.zero;
private void Update()
{
var direction = _zero;
if (Input.GetKey(KeyCode.W))
direction += _fwd;
if (Input.GetKey(KeyCode.S))
direction += _back;
if (Input.GetKey(KeyCode.A))
direction += _left;
if (Input.GetKey(KeyCode.D))
direction += _right;
var rot = Quaternion.LookRotation(direction);
transform.rotation = rot;
}