I am trying to get my 2d character to move up, down, left and right via 4 Arrows I have created as GUI Textures on screen.
I have been at this script for hours, and at first my problem was that the 4 public variables I have used to attach my GUI textures to were not showing up. Now, unity will not even let me attach my script to my player object.
I found a script from here Touch Tutorial and have built on it to include up and down as well as left and right, then removed the jump function as I did not need it.
As I said, I have been at this for hours now and seem to be making it worse, so please if anyone can see where my problem is, please please help!
using UnityEngine;
using System.Collections;
[RequireComponent(typeof (Rigidbody2D))]
[RequireComponent(typeof(BoxCollider2D))]
public class TouchControls : MonoBehaviour {
public GUITexture guiLeft;
public GUITexture guiRight;
public GUITexture guiUp;
public GUITexture guiDown;
// Movement variables
public float moveSpeed = 5f;
// Movement flags
private bool moveLeft, moveRight, moveUp, moveDown = false;
// Update is called once per frame
void Update () {
// Check to see if the screen is being touched
if (Input.touchCount > 0)
{
// Get the touch info
Touch t = Input.GetTouch(0);
// Did the touch action just begin?
if (t.phase == TouchPhase.Began)
{
// Are we touching the left arrow?
if (guiLeft.HitTest(t.position, Camera.main))
{
Debug.Log("Touching Left Control");
moveLeft = true;
}
// Are we touching the right arrow?
if (guiRight.HitTest(t.position, Camera.main))
{
Debug.Log("Touching Right Control");
moveRight = true;
}
// Are we touching the up arrow?
if (guiUp.HitTest(t.position, Camera.main))
{
Debug.Log("Touching Up Control");
moveUp = true;
}
// Are we touching the Down arrow?
if (guiDown.HitTest(t.position, Camera.main))
{
Debug.Log("Touching Down Control");
moveDown = true;
}
}
// Did the touch end?
if (t.phase == TouchPhase.Ended)
{
// Stop all movement
moveUp = moveDown = moveLeft = moveRight = false;
rigidbody2D.velocity = Vector2.zero;
}
}
// Is the left mouse button down?
if (Input.GetMouseButtonDown(0))
{
// Are we clicking the left arrow?
if (guiLeft.HitTest(Input.mousePosition, Camera.main))
{
Debug.Log("Touching Left Control");
moveLeft = true;
}
// Are we clicking the right arrow?
if (guiRight.HitTest(Input.mousePosition, Camera.main))
{
Debug.Log("Touching Right Control");
moveRight = true;
}
// Are we clicking the up arrow?
if (guiUp.HitTest(Input.mousePosition, Camera.main))
{
Debug.Log("Touching Up Control");
moveUp = true;
}
// Are we clicking the Down arrow?
if (guiDown.HitTest(Input.mousePosition, Camera.main))
{
Debug.Log("Touching Down Control");
moveDown = true;
}
}
if (Input.GetMouseButtonUp(0))
{
// Stop all movement on left mouse button up
moveUp = moveDown = moveLeft = moveRight = false;
rigidbody2D.velocity = Vector2.zero;
}
}
void FixedUpdate()
{
// Set velocity based on our movement flags.
if (moveLeft)
{
rigidbody2D.velocity = -Vector2.right * moveSpeed;
}
if (moveRight)
{
rigidbody2D.velocity = Vector2.right * moveSpeed;
}
if (moveUp)
{
rigidbody2D.velocity = Vector2.up * moveSpeed;
}
if (moveDown)
{
rigidbody2D.velocity = -Vector2.up * moveSpeed;
}
}
}