Could someone please help with 2D touch controls?
Hi I am making a 2D platform game and I have the general mechanics in place but I am trying to convert the movement controls to touch but for the life of me I cannot get it to work! I have tried following about 5 different tutorials and have even tried to write a totally separate movement script and link the touch controls to this but it just doesn't seem to recognise the touch buttons. The buttons have been added to the editor as UI Images and they are children of an empty Touch Controls object, which in turn is a child of the canvas. I have my movement script set and working but no matter what I try the touch script will not work and it is driving me crazy!!! Here is the movement part of my controls script
public void InputHandler()
{
movedirection = Input.GetAxisRaw("Horizontal");
if (grounded && movedirection < 0)
{
if (!this.anim.GetCurrentAnimatorStateInfo(0).IsTag("Attack"))
{
rb.velocity = new Vector2(-movespeed, rb.velocity.y);
anim.SetBool("IsRunningLeft", true);
}
}
else if (grounded && movedirection == 0)
{
anim.SetBool("IsRunningLeft", false);
anim.SetBool("IsRunningRight", false);
}
if (grounded && movedirection > 0)
{
if (!this.anim.GetCurrentAnimatorStateInfo(0).IsTag("Attack"))
{
rb.velocity = new Vector2(movespeed, rb.velocity.y);
anim.SetBool("IsRunningRight", true);
}
}
if (IsDead)
{
rb.velocity = Vector2.zero;
}
}
this is on the player. Then I have the most recent attempt at linking the touch controls in the player control script
public void TouchMove(float movedirection)
{
if (moveleft)
{
rb.velocity = new Vector2(-movespeed, rb.velocity.y);
}
else if (moveright)
{
rb.velocity = new Vector2(movespeed, rb.velocity.y);
}
}
and then I have the Touch script itself which is attached to the Touch Controls game object
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Touch : MonoBehaviour
{
private NewPlayControl player;
// Use this for initialization
void Start()
{
player = FindObjectOfType<NewPlayControl>();
}
public void LeftArrow()
{
player.moveleft = true;
player.moveright = false;
}
public void RightArrow()
{
player.moveright = true;
player.moveleft = false;
}
public void ReleaseArrow()
{
player.moveright = false;
player.moveleft = false;
}
}
and I have these functions mapped to the UI Images as Event Triggers.
As mentioned this is the most recent attempt and I have tried various other methods so I would really appreciate any help that can be offered to get past this issue.
Thanks in advance