Hey, so I’ve been playing around with a game I’m working on and have been trying to use a joystick to control attacks, see I’m having the issue that my joystick rotation doesn’t always work as when my character is rotated 180 degrees the way the joystick works changes and it becomes really annoying(Sorry I’m having a hard time explaining the problem)
See, I want to have it so that the left joystick moves the character and the character faces the direction it moves then the other joystick controls the attacks(point the joystick in the direction you want to attack) but my attacks are going to be a trigger that becomes enabled and these are relative to the player, so if the player is rotated 45 degrees that to attack forwards the left joystick must point to 45 degrees, if the player is rotated 180 degrees then to attack forwards the user must point the joystick downwards to attack forwards relative to the character.
I’m not sure if that made sense so this is a demo of how the system works at the moment, but the issue is that when you rotate 180 degrees(Only Up, Up/Right ,Right, Down are implemented ATM) the way the rotation is displayed changes so the code no longer works. (With a keyboard the issue can be shown using WASD for movement and arrow keys for attacking)
This is my attack code(Animation stuff is just commented out just incase some kind person puts it in a scene):
#pragma strict
var angle : float;
var angle2 : float;
var angle3 : float;
var pause : float = 0.325;
var attacking : boolean;
var forwardAttack : GameObject;
var forwardRightAttack : GameObject;
var RightAttack : GameObject;
var BackwardsRightAttack : GameObject;
//var Character : GameObject;
var attackAngle : float = 18;
function Update () {
if(Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0){
// Character.animation.CrossFade("RunCycle");
angle2 = Mathf.Atan2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical")) * Mathf.Rad2Deg;
}
else if(attacking == false){
// Character.animation.CrossFade("Idle");
}
angle3 = Mathf.Atan2(Input.GetAxis("AttackH"), Input.GetAxis("AttackV")) * Mathf.Rad2Deg;
angle = angle3 -= angle2;
if (attacking == true){return;}
if(Input.GetAxis("AttackH") != 0 || Input.GetAxis("AttackV") != 0){
if(angle <= attackAngle angle >= -attackAngle){ //Forward Attack
FwdAttack();
attacking = true;
return;
}
else if(angle <= 90 - attackAngle angle >= attackAngle){ //Forward Right Attack
FrtAttack();
attacking = true;
return;
}
else if(angle <= 90 + attackAngle angle >= 90 - attackAngle){ //Right Attack
RgtAttack();
attacking = true;
return;
}
else if(angle <= 180 - attackAngle angle >= 90 + attackAngle){ //Backwards Right Attack
BrtAttack();
attacking = true;
return;
}
}
}
function FwdAttack(){
forwardAttack.SetActive(true);
yield WaitForSeconds (pause);
forwardAttack.SetActive(false);
attacking = false;
}
function FrtAttack(){
forwardRightAttack.SetActive(true);
yield WaitForSeconds (pause);
forwardRightAttack.SetActive(false);
attacking = false;
}
function RgtAttack(){
RightAttack.SetActive(true);
yield WaitForSeconds (pause);
RightAttack.SetActive(false);
attacking = false;
}
function BrtAttack(){
BackwardsRightAttack.SetActive(true);
yield WaitForSeconds (pause);
BackwardsRightAttack.SetActive(false);
attacking = false;
}