Hello all, I’m a newbie in unity and scriptin’ but I do my best…
I’m developing a simple fallin’ game where my “Player” only needs to move < and >…
It has a gravity of 0,4 and every time I push guiTex my player stops instead of movin’ < or > and goes to 0 velocity again… I’m stuck with a simple movement script, plz help
using UnityEngine;
using System.Collections;
[RequireComponent(typeof (Rigidbody2D))]
[RequireComponent(typeof(BoxCollider2D))]
public class TouchControls : MonoBehaviour
{
public GUITexture guiLeft;
public GUITexture guiRight;
public float moveSpeed = 5f;
private bool moveLeft, moveRight = false;
void Update () {
if (Input.touchCount > 0)
{
Touch t = Input.GetTouch(0);
if (t.phase == TouchPhase.Began)
{
if (guiLeft.HitTest(t.position))
{
moveLeft = true;
}
if (guiRight.HitTest(t.position))
{
moveRight = true;
}
}
if (t.phase == TouchPhase.Ended)
{
moveLeft = moveRight = false;
rigidbody2D.velocity = Vector2.zero;
}
}
if (Input.GetMouseButtonDown(0))
{
if (guiLeft.HitTest(Input.mousePosition))
{
moveLeft = true;
}
if (guiRight.HitTest(Input.mousePosition))
{
moveRight = true;
}
}
if (Input.GetMouseButtonUp(0))
{
moveLeft = moveRight = false;
rigidbody2D.velocity = Vector2.zero;
}
}
void FixedUpdate()
{
if (moveLeft)
{
rigidbody2D.velocity = -Vector2.right * moveSpeed;
}
if (moveRight)
{
rigidbody2D.velocity = Vector2.right * moveSpeed;
}
}
}