- Home /
How to remove the Vertical axis on Joystick?
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;
}
}
}
Answer by pauldarius98 · Feb 26, 2021 at 12:35 PM
The problem is at line 17 where you calculate the local position for x and y (y should be constant and 0 most likely) so the code should look like this:
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;
var maxXValue = pad.rect.width * 0.3f;
var xPosition = Mathf.Clamp(eventData.position.x - pad.position.x, -maxXValue, maxXValue)
transform.localPosition = new Vector2(xPosition, 0f);
//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;
}
}
As you can see, I only calculated the x local position and for y I just pass the value 0f. Hope this helps
Your answer
Follow this Question
Related Questions
Using Right Joystick to Select Weapon in Weapon Wheel 1 Answer
Character Movement w/ Both Joysticks 0 Answers
Lock Joystick Axis 0 Answers
Gamecube Controller joystick axis does not work? 0 Answers
UI Navigation with gamepad 7 Answers