接上一篇 对多个模型环形旋转进行优化 指定旋转位置
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
public class ModelAnimal : MonoBehaviour
{
//记录鼠标滑动
public Vector2 lastPos;//鼠标上次位置
Vector2 currPos;//鼠标当前位置
Vector2 offset;//两次位置的偏移值
//实例化
public static ModelAnimal instance;
//需滑动的物体坐标
public List vertices = new List();
//需滑动的物体
public GameObject[] objArry;
private void Awake()
{
instance = this;for (int i = 0; i < objArry.Length; i++){vertices.Add(objArry[i].transform.localPosition);}
}
void Update()
{DragModel();
}
/// <summary>
/// 拖拽事件
/// </summary>
public void DragModel()
{ if (Input.GetMouseButtonDown(0)){lastPos = Input.mousePosition;Debug.Log(lastPos);}if (Input.GetMouseButtonUp(0)){currPos = Input.mousePosition;offset = currPos - lastPos;Debug.Log(currPos);DoMatch(offset);}
}
/// <summary>
/// 鼠标左滑事件
/// </summary>
public void AnimatorMoveLeft()
{Vector3 finalPos = vertices[objArry.Length - 1];for (int i = objArry.Length - 1; i >= 0; i--){if (1 <= i){objArry[i].transform.DOLocalMove (vertices[i - 1], 2.0f);}else if (0 == i){//objArry[i].transform.localPosition = finalPos;objArry[i].transform.DOLocalMove( finalPos,2.0f);}}GameObject temp = objArry[0];for (int i = 0; i < objArry.Length; i++){if(i < objArry.Length - 1){objArry[i] = objArry[i + 1];}else if(i == objArry.Length - 1){objArry[i] = temp;} }instance.GetComponent<ModelAnimal>().enabled = false;
}/// <summary>
/// 鼠标右滑事件
/// </summary>
public void AnimatorMoveRight()
{ Vector3 Lastpos = vertices[0];for (int i = 0; i <= objArry.Length - 1; i++){if (i < objArry.Length - 1){//objArry[i].transform.localPosition = objArry[i + 1].transform.localPosition;objArry[i].transform.DOLocalMove(vertices[i + 1], 2.0f);}else if (i == objArry.Length - 1){//objArry[i].transform.localPosition = Lastpos;objArry[i].transform.DOLocalMove(Lastpos, 2.0f);} }GameObject temp= objArry[objArry.Length - 1];for (int i = objArry.Length - 1; i >0; i--){ objArry[i] = objArry[i-1];}objArry[0] = temp;instance.GetComponent<ModelAnimal>().enabled = false;
}/// <summary>
/// 移动方向判断
/// </summary>
/// <param name="_offset"></param>
void DoMatch(Vector2 _offset)
{//水平移动if (Mathf.Abs(offset.x) > Mathf.Abs(offset.y)){Debug.Log(offset.x);if (offset.x > 0){Debug.Log("右");AnimatorMoveRight();}else{Debug.Log("左");AnimatorMoveLeft();//sequence.Append(AnimatorMoveLeft());}}else//垂直移动{if (offset.y > 0){Debug.Log("上");}else{Debug.Log("下");}}
}
IEnumerator PlayerAttack()
{yield return new WaitForSeconds(2.0f);Debug.Log("After 2s");
}
}