C# 虚拟操纵杆在触摸时立即向右移动

C# 虚拟操纵杆在触摸时立即向右移动,c#,unity3d,C#,Unity3d,当我用鼠标触摸虚拟操纵杆时,它会立即跳到右边 因此,我必须在屏幕上向左拖动鼠标,将操纵杆拖回中间位置 下面是它的图片: 我的代码如下所示: public class VirtualJoystickController : Monobehaviour, IDragHandler, IPointerUpHandler, IPointerDownHandler { private Vector3 inputVector = Vector3.zero; // the movementD

当我用鼠标触摸虚拟操纵杆时,它会立即跳到右边

因此,我必须在屏幕上向左拖动鼠标,将操纵杆拖回中间位置

下面是它的图片:

我的代码如下所示:

    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));
        }
    }
}
公共类VirtualJoystickController:MonoBehavior、IDragHandler、IPInterUpHandler、IPInterDownHandler
{
私有向量3 inputVector=Vector3.zero;//移动方向
私有图像joystickBackgroundImage=GameObject.FindGameObjectWithTag(“joystickBackgroundImage”).GetComponent();//操纵手柄背景
私有图像joystickImage=GameObject.FindGameObjectWithTag(“操纵杆”).GetComponent();//要使用的操纵杆对象
POINTERDOWN上的公共虚拟无效(PointerEventData e)//单击操纵杆
{
翁德拉(e);
}
公共虚拟void OnPointerUp(PointerEventData e)//离开操纵杆
{
inputVector=Vector3.zero;//重置操纵杆
joystickImage.rectTransform.anchoredPosition=Vector3.0;
}
public virtual void OnDrag(PointerEventData e)//拖动操纵杆
{
矢量2位置;
如果(RectTransformUtility.ScreenPointToLocalPointInRectangle(joystickBackgroundImage.rectTransform,e.position,e.pressEventCamera,out position))//开始拖动它
{
position.x=(position.x/joystickBackgroundImage.rectcransform.sizeDelta.x);
position.y=(position.y/joystickBackgroundImage.rectTransform.sizeDelta.y);
输入向量=新向量3(位置x*2+1,0,位置y*2-1);
inputVector=inputVector.magnitude>1?inputVector.normalized:inputVector;
joystickImage.rectTransform.anchoredPosition=新矢量3(
inputVector.x*(joystickBackgroundImage.rectTransform.sizeDelta.x/3),
inputVector.z*(joystickBackgroundImage.rectcransform.sizeDelta.y/3);
}
}
}
下面是代码

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;
}
private Image joystickBackgroundImage;
私人形象;
公共向量3输入方向{set;get;}
私有void Start()
{
joystickBackgroundImage=GetComponent();
joystickImage=transform.GetChild(0.GetComponent();
输入方向=矢量3.0;
}
公共虚拟void OnDrag(PointerEventData-Ped)
{
矢量2位置=矢量2.0;
if(RectTransformUtility.ScreenPointToLocalPointInRectangle(joystickBackgroundImage.rectTransform,Ped.position,Ped.pressEventCamera,out pos))
{
pos.x=(pos.x/joystickBackgroundImage.rectcransform.sizeDelta.x);
pos.y=(pos.y/joystickBackgroundImage.rectcransform.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;
输入方向=新矢量3(x,0,y);
InputDirection=(InputDirection.magnitude>1)?InputDirection.normalized:InputDirection;
joystickImage.rectTransform.anchoredPosition=新矢量3(InputDirection.x*(joystickBackgroundImage.rectTransform.sizeDelta.x/3),InputDirection.z*(joystickBackgroundImage.rectTransform.sizeDelta.y/3));
}
}
POINTERDOWN上的公共虚拟无效(PointerEventData Ped)
{
昂德拉格(Ped);
}
POINTERUP上的公共虚拟无效(PointerEventData Ped)
{
输入方向=矢量3.0;
joystickImage.rectTransform.anchoredPosition=Vector3.0;
}
您需要将枢轴点设置为与中的位置相同,或为操纵杆容器设置预设