using System.Collections; using System.Collections.Generic; using UnityEngine; using DG.Tweening; using UnityEngine.AI; using UnityEngine.EventSystems; using System; public class SwipeMove : MonoBehaviour { public float MaxSpeed = 10; public float DefoultSpeed = 10; public float SPEED = 10; public float SwipeMoveSide = 0.5f; public float SwipeMoveForward = 0.5f; public float SwipeMoveBack = 0.5f; public RunnersController RunnerController; private NavMeshAgent Agent; public void Start() { RunnerController = GetComponent(); Agent = GetComponent(); } void UpdateDestination() { //Agent.SetDestination(Core.Instance.GetComponent().ListLevelTargets[Core.Instance.GetComponent().CurTarget].position); RunnerController.Destination(); } public void SlideBackward() { timer = defaultTimerValue; DirSwipe = DirectionSwipe.Back; Agent.speed -= 0.5f; } public void SlideForward() { timer = defaultTimerValue; DirSwipe = DirectionSwipe.Forward; Agent.speed += 0.5f; } public void SlideLeft() { timer = defaultTimerValue; DirSwipe = DirectionSwipe.Left; } public void SlideRight() { timer = defaultTimerValue; DirSwipe = DirectionSwipe.Right; } public enum DirectionSwipe { Left, Right, Forward, Back, None } public void SlideNone() { DirSwipe = DirectionSwipe.None; } public DirectionSwipe DirSwipe = DirectionSwipe.None; private float defaultTimerValue = 0.3f; private float timer; private bool GettingControlPlayer; private void Update() { if(Core.Instance.IsGameMode != Core.StateModeGame.IsGameMode ) { return; } if(Agent.speed <=0) { UIManager.Instance.GameOverPanel.gameObject.SetActive(true); UIManager.Instance.GameOverPanel.DOFade(1, 0.5f); return; } Agent.speed -= Time.deltaTime * 0.1f; if(UIManager.Instance.RacePanel.gameObject.activeSelf && Core.Instance.IsGameMode== Core.StateModeGame.IsGameMode && RunnerController.gameObject.activeSelf) { timer -= Time.deltaTime; //switch(DirSwipe) //{ // case DirectionSwipe.Left: // if(timer>0) // { // this.transform.position = Vector3.Slerp(this.transform.position, SwipeMoveSide * -transform.right + this.transform.position, 3f * Time.fixedDeltaTime); // return; // } // //this.transform.Translate(SPEED * Vector3.right * Time.deltaTime, Space.Self); // break; // case DirectionSwipe.Right: // if (timer > 0) // { // this.transform.position = Vector3.Slerp(this.transform.position, SwipeMoveSide * transform.right + this.transform.position, 3f * Time.fixedDeltaTime); // return; // } // //this.transform.Translate(SPEED * Vector3.left * Time.deltaTime, Space.Self); // break; // case DirectionSwipe.Forward: // if (timer > 0) // { // this.transform.position = Vector3.Slerp(this.transform.position, SwipeMoveForward * transform.forward + this.transform.position, 3f * Time.fixedDeltaTime); // return; // } // //this.transform.Translate(SPEED * Vector3.forward * Time.deltaTime, Space.Self); // break; // case DirectionSwipe.Back: // if (timer > 0) // { // this.transform.position = Vector3.Slerp(this.transform.position, SwipeMoveBack * -transform.forward + this.transform.position, 3f * Time.fixedDeltaTime); // return; // } // //this.transform.Translate(SPEED * Vector3.back * Time.deltaTime, Space.Self); // break; //} //UpdateDestination(); //DirSwipe = DirectionSwipe.None; } if (Core.Instance.IsGameMode != Core.StateModeGame.Kiss && Core.Instance.IsGameMode != Core.StateModeGame.StartLevel) { if (RunnerController.IsPlayer) { if (Input.GetMouseButtonDown(0)) { this.OnMouseDown(); this.GettingControlPlayer = true; } if (Input.GetMouseButtonUp(0)) { this.GettingControlPlayer = false; } if (GettingControlPlayer) { this.OnMouseDrag(); } } } //if (Input.touchCount > 0) //{ // Touch touch = Input.GetTouch(0); // get first touch since touch count is greater than zero // if (touch.phase == TouchPhase.Stationary || touch.phase == TouchPhase.Moved) // { // // get the touch position from the screen touch to world point // Vector3 touchedPos = Camera.main.ScreenToWorldPoint(new Vector3(touch.position.x, touch.position.y, 10)); // // lerp and set the position of the current object to that of the touch, but smoothly over time. // transform.position = Vector3.Lerp(transform.position, touchedPos, Time.deltaTime); // } //} } private Vector3 screenPoint; private Vector3 offset; void OnMouseDown() { screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position); offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, screenPoint.y, screenPoint.z)); } void OnMouseDrag() { Vector3 cursorPoint = new Vector3(Input.mousePosition.x, screenPoint.y, screenPoint.z); Vector3 cursorPosition = Camera.main.ScreenToWorldPoint(cursorPoint) + offset; transform.position = Vector3.Lerp(transform.position,cursorPosition, 2* Time.deltaTime); //GetComponent().SetFloat("Blend", 0.0f + ); } private enum DraggedDirection { Up, Down, Right, Left } private DraggedDirection GetDragDirection(Vector3 dragVector) { float positiveX = Mathf.Abs(dragVector.x); float positiveY = Mathf.Abs(dragVector.y); DraggedDirection draggedDir; draggedDir = (dragVector.x > 0) ? DraggedDirection.Right : DraggedDirection.Left; //if (positiveX > positiveY) //{ //} //else //{ // draggedDir = (dragVector.y > 0) ? DraggedDirection.Up : DraggedDirection.Down; //} Debug.Log(draggedDir); return draggedDir; } public void OnEndDrag(PointerEventData eventData) { Debug.Log("Press position + " + eventData.pressPosition); Debug.Log("End position + " + eventData.position); Vector3 dragVectorDirection = (eventData.position - eventData.pressPosition).normalized; Debug.Log("norm + " + dragVectorDirection); GetDragDirection(dragVectorDirection); } }