在Pico中根据Pico手柄位置判定顺/逆时针,其他设备上指定手柄改一下就行了。
不是100%准确目前来说满足现在需求了,也试过其他方式现在这个方式准确度要高一点。
各位大佬要是有耿准确的计算方式,请留言探讨改进。
using Pvr_UnitySDKAPI;
using System.Collections.Generic;
using UnityEngine;
public class MouseJudgeDrawCircle : MonoBehaviour
{
/// <summary>
/// 路径点
/// </summary>
public List<Vector2> mDrawPos = new List<Vector2>();
//Pico要设置成True
public bool IsPico = true;
/// <summary>
/// 按键
/// </summary>
public static Pvr_KeyCode TRIGGER = Pvr_KeyCode.TRIGGER;
/// <summary>
/// 手柄
/// </summary>
public ControllerVariety controller = ControllerVariety.Controller1;
/// <summary>
/// 存储路径点间隔
/// </summary>
private int mInterial = 3;
/// <summary>
/// 画出的方向
/// </summary>
private string _drawType;
/// <summary>
/// 控制是否执行
/// </summary>
private bool _isEditor = true;
/// <summary>
/// 头
/// </summary>
private Transform mPvrHead;
/// <summary>
/// 面
/// </summary>
private Transform mDrawCirclePlane;
/// <summary>
/// 点
/// </summary>
private Transform mDrawCirclePoint;
private Pvr_Controller _Controller;
GameObject obj;
GameObject obja;
GameObject objb;
void Start()
{
mPvrHead = GameObject.Find("Pvr_UnitySDK/Head").transform;
mDrawCirclePlane = GameObject.Find("Pvr_UnitySDK/DrawCirclePlane").transform;
mDrawCirclePoint = mDrawCirclePlane.transform.Find("Point");
}
/// <summary>
/// 添加鼠标路径点
/// </summary>
/// <param name="pos"></param>
private void AddDrawPos(Vector2 pos)
{
if (mInterial == 0)
{
if (!mDrawPos.Exists(X => X.x == pos.x))
{
if (mDrawPos.Count > 1)
{
if (Vector3.Distance(mDrawPos[mDrawPos.Count - 1], pos) > 0.1f)
{
mDrawPos.Add(pos);
}
}
else
{
mDrawPos.Add(pos);
}
}
mInterial = 3;
}
else
{
mInterial--;
}
}
void Update()
{
if (_Controller == null)
{
_Controller = GameObject.Find("Pvr_UnitySDK/ControllerManager").GetComponent<Pvr_Controller>();
}
Vector3 tempPos = new Vector3(mPvrHead.forward.x, 0, mPvrHead.forward.z) + mDrawCirclePlane.position;
mDrawCirclePlane.LookAt(tempPos);
//mPlane.forward = new Vector3(mHead.forward.x, mPlane.position.y, mHead.forward.z);
mDrawCirclePoint.position = _Controller.controller1.transform.position;
if (Input.GetMouseButtonDown(0) || Controller.UPvr_GetKeyDown(1, TRIGGER) /*Input.GetMouseButtonDown(0)*/)
{
//if (obj != null)
//{
// Destroy(obj);
// Destroy(obja);
// Destroy(objb);
//}
if (IsPico)
{
AddDrawPos(mDrawCirclePoint.localPosition);
}
else
{
AddDrawPos(Input.mousePosition);
}
}
//pico直接运行
if (_isEditor)
{
if (Input.GetMouseButton(0) || Controller.UPvr_GetKey(1, TRIGGER) /*Input.GetMouseButton(0)*/)
{
if (IsPico)
{
AddDrawPos(mDrawCirclePoint.localPosition);
}
else
{
AddDrawPos(Input.mousePosition);
}
}
if (Input.GetMouseButtonUp(0) || Controller.UPvr_GetKeyUp(1, TRIGGER) /*Input.GetMouseButtonUp(0)*/)
{
if (IsPico)
{
AddDrawPos(mDrawCirclePoint.localPosition);
}
else
{
AddDrawPos(Input.mousePosition);
}
if (mDrawPos.Count >= 6)
{
int centreValue = Mathf.CeilToInt(mDrawPos.Count / 3);
Vector3 tempDrawBegin = new Vector3(mDrawPos[0].x, mDrawPos[0].y, 0);
Vector3 tempDrawCentre = new Vector3(mDrawPos[centreValue].x, mDrawPos[2].y, 0);
Vector3 tempDrawEnd = new Vector3(mDrawPos[centreValue * 2].x, mDrawPos[5].y, 0);
Vector3 tempOne = tempDrawCentre - tempDrawBegin;
Vector3 tempTwo = tempDrawEnd - tempDrawBegin;
if (tempTwo.y > 0.05f || tempTwo.y < -0.05f)
{
//路径球
//obj = GameObject.CreatePrimitive(PrimitiveType.Sphere);
//obj.GetComponent<Renderer>().material.color = Color.black;
//obj.transform.SetParent(mPlane);
//obj.transform.localScale = new Vector3(0.05f, 0.05f, 0.05f);
//obj.transform.localPosition = mDrawPos[0];
//obj.transform.SetParent(mHead.parent);
//obja = GameObject.CreatePrimitive(PrimitiveType.Sphere);
//obja.GetComponent<Renderer>().material.color = Color.yellow;
//obja.transform.SetParent(mPlane);
//obja.transform.localScale = new Vector3(0.05f, 0.05f, 0.05f);
//obja.transform.localPosition = mDrawPos[centreValue];
//obja.transform.SetParent(mHead.parent);
//objb = GameObject.CreatePrimitive(PrimitiveType.Sphere);
//objb.GetComponent<Renderer>().material.color = Color.red;
//objb.transform.SetParent(mPlane);
//objb.transform.localScale = new Vector3(0.05f, 0.05f, 0.05f);
//objb.transform.localPosition = mDrawPos[centreValue * 2];
//objb.transform.SetParent(mHead.parent);
Vector3 tempCross = Vector3.Cross(tempOne, tempTwo);
if (tempCross.z > 0)
{
_drawType = "逆时针";
}
else
{
_drawType = "顺时针";
}
Debug.Log("_drawType:" + _drawType);
}
}
// 数据初始化
mDrawPos.Clear();
}
}
}
/// <summary>
/// 获取画的类型
/// </summary>
public string GetDrawType
{
get
{
return _drawType;
}
set
{
_drawType = value;
}
}
}