2017-06-24 35 views
1

バーチャルジョイスティックをマウスでタッチすると、すぐに右にジャンプします。バーチャルジョイスティックは、タッチするとすぐに右に移動します

私は、マウスを画面の左側にドラッグして、ジョイスティックを中央にドラッグする必要があります。ここで

はそれの写真です:

joystick view

私のコードは、このなります

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)); 
     } 
    } 
} 

答えて

1

ここでuは同じピボットポイントを設定する必要があるコード

private Image joystickBackgroundImage; 
private Image joystickImage; 

public Vector3 InputDirection { set; get; } 

private void Start() 
{ 
    joystickBackgroundImage = GetComponent<Image>(); 
    joystickImage = transform.GetChild(0).GetComponent<Image>(); 
    InputDirection = Vector3.zero; 
} 

public virtual void OnDrag(PointerEventData Ped) 
{ 
    Vector2 pos = Vector2.zero; 
    if (RectTransformUtility.ScreenPointToLocalPointInRectangle(joystickBackgroundImage.rectTransform, Ped.position, Ped.pressEventCamera, out pos)) 
    { 
     pos.x = (pos.x/joystickBackgroundImage.rectTransform.sizeDelta.x); 
     pos.y = (pos.y/joystickBackgroundImage.rectTransform.sizeDelta.y); 

     float x = (joystickBackgroundImage.rectTransform.pivot.x == 1f) ? pos.x * 2 + 1 : pos.x * 2 - 1; 
     float y = (joystickBackgroundImage.rectTransform.pivot.y == 1f) ? pos.y * 2 + 1 : pos.y * 2 - 1; 

     InputDirection = new Vector3(x, 0, y); 
     InputDirection = (InputDirection.magnitude > 1) ? InputDirection.normalized : InputDirection; 

     joystickImage.rectTransform.anchoredPosition = new Vector3(InputDirection.x * (joystickBackgroundImage.rectTransform.sizeDelta.x/3), InputDirection.z * (joystickBackgroundImage.rectTransform.sizeDelta.y/3)); 
    } 
} 

public virtual void OnPointerDown(PointerEventData Ped) 
{ 
    OnDrag(Ped); 
} 

public virtual void OnPointerUp(PointerEventData Ped) 
{ 
    InputDirection = Vector3.zero; 
    joystickImage.rectTransform.anchoredPosition = Vector3.zero; 
} 

ですがポジションアンコールプリセットジョイスティックコンテナ

+1

これは右のものでした、ありがとうございました:) – Question3r

関連する問題