当我用鼠标触摸虚拟操纵杆时,它会立即跳到右边。
所以我必须将鼠标拖到屏幕左侧,将操纵杆拖回中间。
以下是它的图片:
我的代码看起来像这样:
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));
}
}
}
答案 0 :(得分:1)
这是代码
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;
}
你需要在 Ancor Presets 中为操纵杆容器设置枢轴点与位置相同