- Home /
problem with twin stick shooter controls
I have started working on the basic setup for a twin stick shooter using touch controls. Think hotline miami or geometry wars. I have downloaded the joystick pack found here https://assetstore.unity.com/packages/tools/input-management/joystick-pack-107631
I want the movement and the aiming to be independent of each other. The movement worked perfectly until I added the ability to aim. The left analog stick will now rotate the sprite along the z-axis, which I don't want. When i disable aiming the movement works fine, and when I disable movement the rotation works fine. I'm not sure what the problem is or how to go about fixing it. Any help is appreciated, thanks in advance.
public float moveSpeed;
public VariableJoystick variableJoystick;
public VariableJoystick variableJoystickLook;
public Rigidbody2D rb;
Vector3 lookDirection;
void FixedUpdate() {
//Movement
Vector2 direction = new Vector2(variableJoystick.Horizontal, variableJoystick.Vertical);
transform.Translate(direction * moveSpeed * Time.deltaTime, Space.World);
//Aiming
Vector3 lookDirection = Vector3.up * variableJoystickLook.Vertical + Vector3.right * variableJoystickLook.Horizontal;
transform.right = lookDirection - transform.position;
}
Answer by DylanST · Jan 02, 2020 at 04:36 AM
I managed to solve the problem. The playerPosition variable should have transform.rotation instead of transform.position.
void FixedUpdate() {
//Movement
Vector2 direction = new Vector2(variableJoystick.Horizontal, variableJoystick.Vertical);
transform.Translate(direction * moveSpeed * Time.deltaTime, Space.World);
//Aiming
Vector2 lookDirection = Vector2.up * variableJoystickLook.Vertical + Vector2.right * variableJoystickLook.Horizontal;
Vector2 playerPosition = new Vector2(transform.rotation.x, transform.rotation.y);
transform.right = lookDirection - playerPosition;
}
Answer by QuincyOnIce · Jan 01, 2020 at 11:38 PM
It may be because you're using a Vector3 on lookDirection. Idk, I don't use 2D, but if it's affecting the Z-Axis, it's likely due to a 3rd Dimension.
Unfortunately this did not change anything. I changed the code to this
void FixedUpdate() {
//$$anonymous$$ovement
Vector2 direction = new Vector2(variableJoystick.Horizontal, variableJoystick.Vertical);
transform.Translate(direction * moveSpeed * Time.deltaTime, Space.World);
//Ai$$anonymous$$g
Vector2 lookDirection = Vector2.up * variableJoystickLook.Vertical + Vector2.right * variableJoystickLook.Horizontal;
Vector2 playerPosition = new Vector2(transform.position.x, transform.position.y);
transform.right = lookDirection - playerPosition;
}