Question by
muhammadtahiriqbal · Feb 19, 2016 at 04:44 PM ·
c#uiunity5eventsystem
how to do on single click fire and on drag appears the joystick and get the input from joystick
i have a joystick in my scene
on drag i want to do that take the input from joystick
on single tap fire i try on dragg(event data) and on pointer down but when i single click,the on drag function is called. here is my code public virtual void OnDrag(PointerEventData eventData) {
if (HideOnRelease) { Hide(false); } // Unity remote multitouch related thing // When we feed fake PointerEventData we can't really provide a camera, // it has a lot of private setters via not created objects, so even the Reflection magic won't help a lot here // Instead, we just provide an actual event camera as a public property so we can easily set it in the Input Helper class CurrentEventCamera = eventData.pressEventCamera ?? CurrentEventCamera; // We get the local position of the joystick Vector3 worldJoystickPosition; RectTransformUtility.ScreenPointToWorldPointInRectangle(_stickTransform, eventData.position, CurrentEventCamera, out worldJoystickPosition); // Then we change it's actual position so it snaps to the user's finger _stickTransform.position = worldJoystickPosition; // We then query it's anchored position. It's calculated internally and quite tricky to do from scratch here in C# var stickAnchoredPosition = _stickTransform.anchoredPosition; // Some bitwise logic for constraining the joystick along one of the axis // If the "Both" option was selected, non of these two checks will yield "true" if ((JoystickMoveAxis & ControlMovementDirection.Horizontal) == 0) { stickAnchoredPosition.x = _intermediateStickPosition.x; } if ((JoystickMoveAxis & ControlMovementDirection.Vertical) == 0) { stickAnchoredPosition.y = _intermediateStickPosition.y; } _stickTransform.anchoredPosition = stickAnchoredPosition; // Find current difference between the previous central point of the joystick and it's current position Vector2 difference = new Vector2(stickAnchoredPosition.x, stickAnchoredPosition.y) - _intermediateStickPosition; // Normalisation stuff var diffMagnitude = difference.magnitude; var normalizedDifference = difference / diffMagnitude; // If the joystick is being dragged outside of it's range if (diffMagnitude > MovementRange) { if (MoveBase && SnapsToFinger) { // We move the base so it maps the new joystick center position var baseMovementDifference = difference.magnitude - MovementRange; var addition = normalizedDifference * baseMovementDifference; _baseTransform.anchoredPosition += addition; _intermediateStickPosition += addition; } else { _stickTransform.anchoredPosition = _intermediateStickPosition + normalizedDifference * MovementRange; } } // We don't need any values that are greater than 1 or less than -1 var horizontalValue = Mathf.Clamp(difference.x * _oneOverMovementRange, -1f, 1f); var verticalValue = Mathf.Clamp(difference.y * _oneOverMovementRange, -1f, 1f); // Finally, we update our virtual axis HorizintalAxis.Value = horizontalValue; VerticalAxis.Value = verticalValue; } public void OnPointerUp(PointerEventData eventData) { // When we lift our finger, we reset everything to the initial state _baseTransform.anchoredPosition = _initialBasePosition; _stickTransform.anchoredPosition = _initialStickPosition; _intermediateStickPosition = _initialStickPosition; HorizintalAxis.Value = VerticalAxis.Value = 0f; print ("pointer is up"); // We also hide it if we specified that behaviour if (HideOnRelease) { Hide(true); } } public void OnPointerClick(PointerEventData eventData){ print("on pointer click"); } public void OnPointerDown(PointerEventData eventData) {
// fire here
//
// } // We also want to show it if we specified that behaviour // if (HideOnRelease) // { // Hide(false); // } }
/// <summary>
/// Simple "Hide" behaviour
/// </summary>
/// <param name="isHidden">Whether the joystick should be hidden</param>
private void Hide(bool isHidden)
{
JoystickBase.gameObject.SetActive(!isHidden);
Stick.gameObject.SetActive(!isHidden);
}
}
}
Comment
Your answer
Follow this Question
Related Questions
How to send event to my canvas via script 0 Answers
Need alternative for onEndEdit on InputField 2 Answers
Error upon rebooting Unity - "Associated script cannot be loaded" How can I resolve this? 2 Answers
MobileSingleStickControl multitouch not supported. How fix? 0 Answers
My TakeDamage function doesnt work 0 Answers