- Home /
2d JoyStick Controller problems
hello everyone i am trying to create a joystick controller for 2D game which has right , left, and jump movements; i have created right left movements and i tried to create jump while my player is jumping its shaking and i don't know whether i am doing correct if i am doing in a wrong way please help me to create a joystick control with jump i will attach the script
using UnityEngine;
public class Player2DExample : MonoBehaviour { public float moveSpeed = 8f; public Joystick joystick;
void Update()
{
Vector2 moveVector = (Vector2.right * joystick.Horizontal + Vector2.up * joystick.Vertical);
// if (moveVector != Vector3.zero)
{
// transform.rotation = Quaternion.LookRotation(Vector3.forward, moveVector);
transform.Translate(moveVector * moveSpeed * Time.deltaTime, Space.World);
}
}
}
using UnityEngine; using UnityEngine.EventSystems;
public class FixedJoystick : Joystick { Vector2 joystickPosition = Vector2.zero; private Camera cam = new Camera();
void Start()
{
joystickPosition = RectTransformUtility.WorldToScreenPoint(cam, background.position);
}
public override void OnDrag(PointerEventData eventData)
{
Vector2 direction = eventData.position - joystickPosition;
inputVector = (direction.magnitude > background.sizeDelta.x / 2f) ? direction.normalized : direction / (background.sizeDelta.x / 2f);
ClampJoystick();
handle.anchoredPosition = (inputVector * background.sizeDelta.x / 2f) * handleLimit;
}
public override void OnPointerDown(PointerEventData eventData)
{
OnDrag(eventData);
}
public override void OnPointerUp(PointerEventData eventData)
{
inputVector = Vector2.zero;
handle.anchoredPosition = Vector2.zero;
}
}
https://www.youtube.com/watch?v=bp2PiFC9sSs&t=97s . this will help you
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How to use Joystick from Standard Assets? 0 Answers
Distribute terrain in zones 3 Answers