1
バーチャルジョイスティックをマウスでタッチすると、すぐに右にジャンプします。バーチャルジョイスティックは、タッチするとすぐに右に移動します
私は、マウスを画面の左側にドラッグして、ジョイスティックを中央にドラッグする必要があります。ここで
はそれの写真です:
私のコードは、このなります
public class VirtualJoystickController : Monobehaviour, IDragHandler, IPointerUpHandler, IPointerDownHandler
{
private Vector3 inputVector = Vector3.zero; // the movementDirection
private Image joystickBackgroundImage = GameObject.FindGameObjectWithTag("JoystickBackGroundImage").GetComponent<Image>(); // the joysticks background
private Image joystickImage = GameObject.FindGameObjectWithTag("Joystick").GetComponent<Image>(); // the joystick object to use
public virtual void OnPointerDown(PointerEventData e) // Click the joystick
{
OnDrag(e);
}
public virtual void OnPointerUp(PointerEventData e) // leave the joystick
{
inputVector = Vector3.zero; // reset joystick
joystickImage.rectTransform.anchoredPosition = Vector3.zero;
}
public virtual void OnDrag(PointerEventData e) // drag the joystick
{
Vector2 position;
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(joystickBackgroundImage.rectTransform, e.position, e.pressEventCamera, out position)) // start dragging it
{
position.x = (position.x/joystickBackgroundImage.rectTransform.sizeDelta.x);
position.y = (position.y/joystickBackgroundImage.rectTransform.sizeDelta.y);
inputVector = new Vector3(position.x * 2 + 1, 0, position.y * 2 - 1);
inputVector = inputVector.magnitude > 1 ? inputVector.normalized : inputVector;
joystickImage.rectTransform.anchoredPosition = new Vector3(
inputVector.x * (joystickBackgroundImage.rectTransform.sizeDelta.x/3),
inputVector.z * (joystickBackgroundImage.rectTransform.sizeDelta.y/3));
}
}
}
これは右のものでした、ありがとうございました:) – Question3r