Code we are working with Code we are working with:
public bool LeftSideActive(int input)
{
if ((Input.GetTouch(input).position.x >0) && (Input.GetTouch(input).position.x < halfScreenWidth))
{
if(Input.GetMouseButtonDown(0)) {
LeftSideTouched = true;
Debug.Log("Left side enabled: " + LeftSideTouched);
}
if(Input.GetMouseButtonUp(0)) {
LeftSideTouched = false;
Debug.Log("Left side enabled: " + LeftSideTouched);
}
}
else if ((Input.GetTouch(input).position.x >halfScreenWidth && LeftSideTouched)){
if(Input.GetMouseButtonUp(0)) {
LeftSideTouched = false;
}
}
return LeftSideTouched;
}
public bool RightSideActive (int input)
{
if ((Input.GetTouch(input).position.x > halfScreenWidth) && (Input.GetTouch(input).position.x < Screen.width))
{
if(Input.GetMouseButtonDown(0)){
RightSideTouched = true;
Debug.Log("Right side enabled: " + RightSideTouched);
}
if(Input.GetMouseButtonUp(0)) {
RightSideTouched = false;
Debug.Log("Right side enabled: " + RightSideTouched);
}
}
else if ((Input.GetTouch(input).position.x < halfScreenWidth && RightSideTouched)){
if(Input.GetMouseButtonUp(0)) {
RightSideTouched = false;
}
}
return RightSideTouched;
}
public void LeftSideControls(int input){
if ((Input.GetTouch(input).position.x >0) && (Input.GetTouch(input).position.x < halfScreenWidth))
{
if(Input.GetMouseButtonDown(0))
{
LeftSideTouched = true;
movementA = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.transform.position.z));
this.transform.position = movementA; //Updates position where joystick will appear
}
if(Input.GetMouseButtonUp(0))
{
LeftSideTouched = false;
}
// Sets new finger position on drag
movementB = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.transform.position.z));
}
else if ((Input.GetTouch(input).position.x > halfScreenWidth && LeftSideTouched)){
if(Input.GetMouseButtonUp(0)){
}
//Sets new finger position on drag; When finger is dragged from left to right side of the screen controls are kept.
movementB = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.transform.position.z));
}
}
public void RightSideControls(int input){
if ((Input.GetTouch(input).position.x > halfScreenWidth) && (Input.GetTouch(input).position.x < Screen.width))
{
if(Input.GetMouseButtonDown(0)){
RightSideTouched = true;
shootingA = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.transform.position.z));
this.transform.position = shootingA; //Updates position where joystick will appear
}
if(Input.GetMouseButtonUp(0))
{
RightSideTouched = false;
}
shootingB = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.transform.position.z)) - transform.position;
shootingAngle = Mathf.Atan2(shootingB.y,shootingB.x)*Mathf.Rad2Deg;
playerGun.transform.rotation = Quaternion.Euler(0,0,shootingAngle);
}
else if ((Input.GetTouch(input).position.x < halfScreenWidth && RightSideTouched)){
if(Input.GetMouseButtonUp(0)){
RightSideTouched = false;
}
//Sets new finger position on drag; When finger is dragged from right to left side of the screen controls are kept.
shootingB = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.transform.position.z)) - transform.position;
shootingAngle = Mathf.Atan2(shootingB.y,shootingB.x)*Mathf.Rad2Deg;
playerGun.transform.rotation = Quaternion.Euler(0,0,shootingAngle);
}
}
public void JoystickEnabling()
{
while( Input.touchCount > 0)
{
if(Input.touchCount>=3)
{
Debug.Log("More than 2 touches!!!!");
return;
}
if(Input.touchCount == 1)
{
Debug.Log("Touch-count 1");
FirstFinger = 0;
SecondFinger = FirstFinger;
if(LeftSideActive(FirstFinger)&&!RightSideActive(FirstFinger))
{
JoystickSpriteRenderer(moveJoyRenderer, true);
JoystickSpriteRenderer(shootJoyRenderer, false);
LeftSideControls(FirstFinger);
//Debug.Log("LeftSideTouchy");
}
else if(!LeftSideActive(FirstFinger)&&RightSideActive(FirstFinger))
{
JoystickSpriteRenderer(moveJoyRenderer, false);
JoystickSpriteRenderer(shootJoyRenderer, true);
RightSideControls(FirstFinger);
//Debug.Log("RightSideTouchy");
}
else{
JoystickSpriteRenderer(moveJoyRenderer, false);
JoystickSpriteRenderer(shootJoyRenderer, false);
}
}
else if (Input.touchCount == 2)
{
Debug.Log("Touch-count 2");
FirstFinger = 0;
SecondFinger = 1;
if (LeftSideActive(FirstFinger)){
LeftSideControls(FirstFinger);
if(LeftSideActive(SecondFinger)){
Debug.Log("Second left");
return;
}
else if(RightSideActive(SecondFinger)){
Debug.Log("LEft and Right");
RightSideControls(SecondFinger);
}
}
}
break;
}
}
void FixedUpdate(){
if(LeftSideTouched){
//Calculates offset between first and last finger position
Vector2 offset = movementA - movementB;
Vector2 direction = Vector2.ClampMagnitude(offset, 1.0f);
//Moves character
MoveCharacter(direction * -1f);
}
}
void MoveCharacter(Vector2 direction) // Function for character movement
{
player.Translate(direction * speed * Time.deltaTime);
}
//Enable either joystick sprite
void JoystickSpriteRenderer(SpriteRenderer joystick, bool enable){
joystick.GetComponent<SpriteRenderer>().enabled = enable;
}
}