Here are my scripts (I copied them from here - How to Move rigidbody2D on touch ? - Unity Answers
I attached this to my Player object and named the script Player.cs :
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour {
public float PlayerMoveSpeed = 3f;
public static bool PlayerMoveRight = false;
public static bool PlayerMoveLeft = false;
// Use this for initialization
void Update()
{
//Run these to functions
Movement();
}
void Movement()
{
//Touch Controls
if(MoveRight == true)
{
transform.Translate(Vector2.right * PlayerMoveSpeed * Time.deltaTime);
transform.eulerAngles = new Vector2(0, 0);
}
//Left Movement triggered
if(MoveLeft == true)
{
transform.Translate(Vector2.right * PlayerMoveSpeed * Time.deltaTime);
transform.eulerAngles = new Vector2(0, 180);
}
}
}
The variables MoveLeft & MoveRight would be set to true/false with these scripts…
MoveLeft.cs… For Left movement on touch (Attach to guiTexture Called MoveLeft)
using UnityEngine;
using System.Collections;
public class MoveLeft : MonoBehaviour {
// Update is called once per frame
void Update ()
{
//Is there a touch?
if(Input.touches.Length <= 0)
{
//If no touches...
}
else //If screen touched..
{
//Loop through these
for(int i = 0; i < Input.touchCount; i++)
{
//executes this code for current touch
if(this.guiTexture.HitTest(Input.GetTouch(i).position))
{
//If current touch hits the guitexture...
//MoveRight bool within PlayerLogic = false when touch has Began if(Input.GetTouch(i).phase == TouchPhase.Began)
{
Player.PlayerMoveLeft = true;
}
//MoveLeft bool within PlayerLogic = false when touch has ended if(Input.GetTouch(i).phase == TouchPhase.Ended)
{
Player.PlayerMoveLeft = false;
}
}
}
}
}
}
MoveRight.cs… For Right movement on touch (Attach to guiTexture Called MoveRight)
using UnityEngine;
using System.Collections;
public class MoveRight : MonoBehaviour {
// Update is called once per frame
void Update ()
{
//Is there a touch?
if(Input.touches.Length <= 0)
{
//If no touches...
}
else //If screen touched..
{
//Loop through these
for(int i = 0; i < Input.touchCount; i++)
{
//executes this code for current touch
if(this.guiTexture.HitTest(Input.GetTouch(i).position))
{
//If current touch hits the guitexture...
if(Input.GetTouch(i).phase == TouchPhase.Began)
{
Player.PlayerMoveRight = true;
}
if(Input.GetTouch(i).phase == TouchPhase.Ended)
{
Player.PlayerMoveRight = false;
}
}
}
}
}
}
After I’ve done all this it give me two errors that I don’t know how to fix.
Here are the errors:
Assets/Scripts/Player.cs(20,20): error CS0119: Expression denotes a type', where a
variable’, value' or
method group’ was expected
Assets/Scripts/Player.cs(27,20): error CS0119: Expression denotes a type', where a
variable’, value' or
method group’ was expected