public override void OnTouchMoved(TouchEventInfo eventInfo) { Vector3 newPosition = Camera.main.ScreenToWorldPoint(eventInfo.touchPositionPix); newPosition.z = character.transform.position.z; character.transform.position = newPosition; }
private void Update() { TouchPhase touchPhase = TouchPhase.Moved; if (Input.GetMouseButtonDown(0)) { touchPhase = TouchPhase.Began; } else if (Input.GetMouseButtonUp(0)) { touchPhase = TouchPhase.Ended; } Vector3 touchPosition = Input.mousePosition; if (TouchPhase.Began == touchPhase) { // Check if selecting anything that is a input receiver // If so, redirect all input to that input receiver Ray ray = Camera.main.ScreenPointToRay(touchPosition); int numHits = Physics2D.RaycastNonAlloc(ray.origin, ray.direction, _raycastResults, raycastDistance, raycastLayerMask); for (int hitIdx = 0; hitIdx < numHits; ++hitIdx) { RaycastHit2D hit = _raycastResults[hitIdx]; BaseInputReceiver inputReceiver = hit.collider.GetComponent <BaseInputReceiver>(); if (null != inputReceiver) { _currentReceiver = inputReceiver; break; } } } if (null == _currentReceiver) { return; } // Gather mouse position and give it to the current input receiver TouchEventInfo eventInfo = new TouchEventInfo() { touchPositionPix = touchPosition }; switch (touchPhase) { case TouchPhase.Began: _currentReceiver.OnTouchBegan(eventInfo); break; case TouchPhase.Moved: _currentReceiver.OnTouchMoved(eventInfo); break; case TouchPhase.Ended: _currentReceiver.OnTouchEnded(eventInfo); _currentReceiver = null; break; } }
public override void OnTouchBegan(TouchEventInfo eventInfo) { if (null != character.currentRoom) { _characterPreviousRoom = character.currentRoom; character.currentRoom.RemoveCharacter(character); } _initialPosition = character.transform.position; character.isDragged = true; }
public override void OnTouchEnded(TouchEventInfo eventInfo) { // Check if there's a room below Ray ray = Camera.main.ScreenPointToRay(eventInfo.touchPositionPix); int numHits = Physics2D.RaycastNonAlloc(ray.origin, ray.direction, _raycastResults, raycastDistance, raycastLayerMask); Room droppedToRoom = null; for (int hitIdx = 0; hitIdx < numHits; ++hitIdx) { Room room = _raycastResults[hitIdx].collider.GetComponentInParent <Room>(); if (null != room) { droppedToRoom = room; break; } } if ((null != droppedToRoom) && (droppedToRoom.HasCapacity())) { droppedToRoom.AddCharacter(character); Vector3 targetPosition = ray.origin; targetPosition.z = character.transform.position.z; character.targetPosition = droppedToRoom.GetRandomFreeDancePosition(); character.transform.position = targetPosition; if (null != _visitorSpawner) { _visitorSpawner.SpawnReplacementForCharacter(character); } } else { character.transform.position = _initialPosition; if (null != _characterPreviousRoom) { _characterPreviousRoom.AddCharacter(character); } } character.isDragged = false; }
public virtual void OnTouchBegan(TouchEventInfo eventInfo) { }
public virtual void OnTouchEnded(TouchEventInfo eventInfo) { }
// Update is called once per frame void Update() { int numTouches = Input.touches.Length; if (numTouches > 0) { for (int i = 0; i < numTouches; i++) { //pinch needs two touches if (numTouches == 2) { if (i == 0) { //record the first point pinchPosition1 = Input.touches[i].position; pinch1Touch = Input.touches[i]; } else { //record the second point pinchPosition2 = Input.touches[i].position; //call the pinch event if (Pinch != null) { float length = 0; if (lastPinchLength != 0) { //length = Vector2.Distance(pinchPosition1, pinchPosition2) - lastPinchLength; //calculate the normalized distance between both fingers length = Vector2.Distance(normalizeTouchPosition(pinchPosition1), normalizeTouchPosition(pinchPosition2)) - lastPinchLength; } lastPinchLength = Vector2.Distance(normalizeTouchPosition(pinchPosition1), normalizeTouchPosition(pinchPosition2)); if (length > pinchSensitivity || length < -pinchSensitivity) { Pinch(convertTouchInfo(Input.touches[i]), pinch1Touch, Time.time - GetStartInformation(Input.touches[i].fingerId).time, length); } } } } //touch started if (Input.touches[i].phase == TouchPhase.Began) { //DebugOut.Instance.AddDebug("BEGAN FIRED"); Debug.Log("BEGAN FIRED"); //store the start position so there is a delta in the touchphase ended SetStartInformation(Input.touches[i].fingerId, Input.touches[i].position, Time.time); //*************************Event Touch Began if (TouchBegan != null) { TouchBegan(convertTouchInfo(Input.touches[i])); } } //touch moved if (Input.touches[i].phase == TouchPhase.Moved) { //*************************Event Touch Moved if (TouchMoved != null) { TouchMoved(convertTouchInfo(Input.touches[i])); } } //touch ended if (Input.touches[i].phase == TouchPhase.Ended) { //reset pinch if (numTouches == 2) { lastPinchLength = 0; pinchPosition1 = Vector2.zero; pinchPosition2 = Vector2.zero; } //*************************Event TouchEnded if (TouchEnded != null) { TouchEnded(convertTouchInfo(Input.touches[i])); } //if touch ended within a threshold time it is a swipe or a tap if (Time.time - GetStartInformation(Input.touches[i].fingerId).time < gestureTime) { //DebugOut.Instance.AddDebug("GESTURES DISTANCE: " + Vector2.Distance(Input.touches[i].position, GetStartInformation(Input.touches[i].fingerId).position).ToString()); //hasn't moved past threshhold, so it's a tap //normalize touch position Vector2 touchPos = normalizeTouchPosition(Input.touches[i].position); //normalize start information position Vector2 startPos = normalizeTouchPosition(GetStartInformation(Input.touches[i].fingerId).position); //float dist = Vector2.Distance(Input.touches[i].position, GetStartInformation(Input.touches[i].fingerId).position); float dist = Vector2.Distance(touchPos, startPos); //DebugOut.Instance.AddDebug("Distance: " + dist); Debug.Log("Distance: " + dist); if (dist < tapDistanceThreshold) { //*************************Event Tapped if (Tap != null) { Tap(convertTouchInfo(Input.touches[i]), Time.time - GetStartInformation(Input.touches[i].fingerId).time); } } //touch moved so it is a swipe else { //*************************Event Swipe if (Swipe != null) { Swipe(convertTouchInfo(Input.touches[i]), Time.time - GetStartInformation(Input.touches[i].fingerId).time, Input.touches[i].position - GetStartInformation(Input.touches[i].fingerId).position); //m_TouchEventInfo.Remove(Input.GetTouch(i).fingerId); } } } } } } else { //-- no touch, check mouse input isMouseButtonDown = Input.GetMouseButton(0); //-- if the input of the mouse has changed then handle it if (isMouseButtonDown != m_MouseButtonState.mouseButtonDown) { //-- populate the mouse info with the mouse position TouchEventInfo mouseInfo = new TouchEventInfo(); mouseInfo.position = new Vector2(Input.mousePosition.x, Input.mousePosition.y); //-- began fired if (isMouseButtonDown == true) { //-- cache the start time m_MouseButtonState.startTime = Time.time; m_MouseButtonState.position = Input.mousePosition; //-- fire the began event if (TouchBegan != null) { TouchBegan(mouseInfo); } } //-- end fired else { //-- if within the gesture threshhold value, then register as a tap float delta = Time.time - m_MouseButtonState.startTime; if (delta < gestureTime) { if (Tap != null) { Tap(mouseInfo, delta); } } //-- fire ended event if (TouchEnded != null) { m_MouseButtonState.position = Vector3.zero; TouchEnded(mouseInfo); } } m_MouseButtonState.mouseButtonDown = isMouseButtonDown; } else { //-- dragging if (isMouseButtonDown == true) { float delta = Time.time - m_MouseButtonState.startTime; if (delta > gestureTime) { TouchEventInfo mouseInfo = new TouchEventInfo(); mouseInfo.position = new Vector2(Input.mousePosition.x, Input.mousePosition.y); mouseInfo.deltaTime = delta; // -gestureTime; mouseInfo.deltaPosition = (mouseInfo.position - m_MouseButtonState.position); // *(mouseInfo.deltaTime / delta); if (TouchMoved != null) { TouchMoved(mouseInfo); } m_MouseButtonState.position = Input.mousePosition; } } } } }
private void TouchInput_Swipe(TouchEventInfo touchInfo, float deltaTime, Vector2 deltaPosition) { switch (GetSwipeDirectionFromVector(deltaPosition)) { case SwipeDirection.SwipeDown: RequestMove(MoveDirection.MoveDown); break; case SwipeDirection.SwipeUp: RequestMove(MoveDirection.MoveUp); break; case SwipeDirection.SwipeLeft: RequestMove(MoveDirection.MoveLeft); break; case SwipeDirection.SwipeRight: RequestMove(MoveDirection.MoveRight); break; } }
public void MoveRight() { if (!isBusy) { TouchEventInfo temp = new TouchEventInfo(); TouchInput_Swipe(temp, 0, Vector2.right); } }
public void MoveDown() { if (!isBusy) { TouchEventInfo temp = new TouchEventInfo(); TouchInput_Swipe(temp, 0, Vector2.down); } }