Hello! I’m new on this so I don’t realy know how to remove the Vertical movement of my joystick, so I will realy apreciate if someone can help me! here is the code and a picture with my Joystick.In this moment my Joystick can be moved in circle, even if the image shows that is only for right and left movement, and i dont know how to "block"or “stop” the movement in circle and up and down.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class JoyStick : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IDragHandler
{
public RectTransform pad;
public Transform player;
Vector3 moveForward;
Vector3 moveRotate;
public float moveSpeed;
public float rotateSpeed;
public void OnPointerDown(PointerEventData eventData)
{
StartCoroutine("PlayerMove");
}
public void OnPointerUp(PointerEventData eventData)
{
transform.localPosition = Vector3.zero;
moveForward = Vector3.zero;
moveRotate = Vector3.zero;
StopCoroutine("PlayerMove");
}
public void OnDrag(PointerEventData eventData)
{
transform.position = eventData.position;
transform.localPosition = Vector2.ClampMagnitude(eventData.position - (Vector2)pad.position, pad.rect.width * 0.3f);
//moveForward = new Vector3(0, 0, transform.localPosition.y).normalized;
moveRotate = new Vector3(0, transform.localPosition.x, 0).normalized;
}
IEnumerator PlayerMove()
{
while (true)
{
player.Translate(moveForward * moveSpeed * Time.deltaTime);
if (Mathf.Abs(transform.localPosition.x) > pad.rect.width * 0.2f)
player.Rotate(moveRotate * rotateSpeed * Time.deltaTime);
yield return null;
}
}
}