Pico老版SDK为我们提供了很多的API,但是手柄上的很多功能需要我们自己编写。这篇文章记录一下手柄的一些常用控制功能。
1.手柄射线以及射线检测。
2.手柄的触碰。
3.主手柄切换。
using System;
using UnityEngine;
using System.Collections;
using Pvr_UnitySDKAPI;
using UnityEngine.SceneManagement;
public class Pvr_ControllerHand : MonoBehaviour
{
public GameObject controller0;
public GameObject controller1;
[HideInInspector]
public GameObject currentController;
private GameObject rayLine;
private GameObject dot;
private GameObject ray_alpha;
void Start()
{
Sensor.UPvr_OptionalResetSensor(0, 1, 1);
SetMainHand(controller1);
Controller.UPvr_SetMainHandNess(1);
}
void Update()
{
if (Controller.UPvr_GetKeyDown(0, Pvr_KeyCode.TRIGGER)
|| Input.GetKeyDown(KeyCode.A)
|| Controller.UPvr_GetKeyDown(0, Pvr_KeyCode.X)
|| Controller.UPvr_GetJoystickUp(0)
)
{
Debug.Log("切换==========" + 0);
Controller.UPvr_SetMainHandNess(0);
SetMainHand(controller0);
}
if (Controller.UPvr_GetKeyDown(1, Pvr_KeyCode.TRIGGER)
|| Input.GetKeyDown(KeyCode.B)
|| Controller.UPvr_GetKeyDown(1, Pvr_KeyCode.A)
|| Controller.UPvr_GetJoystickUp(1)
)
{
Debug.Log("切换==========" + 1);
Controller.UPvr_SetMainHandNess(1);
SetMainHand(controller1);
}
if (Controller.UPvr_GetJoystickUp(1)
|| Controller.UPvr_GetJoystickUp(0)
|| Input.GetMouseButton(0)
)
{
if (dot != null)
dot.SetActive(false);
if (rayLine != null)
rayLine.SetActive(false);
if (ray_alpha != null)
ray_alpha.SetActive(false);
}
if (!Controller.UPvr_GetJoystickUp(1)
&& !Controller.UPvr_GetJoystickUp(0)
|| Input.GetMouseButtonUp(0)
)
{
if (dot != null)
dot.SetActive(true);
if (rayLine != null)
rayLine.SetActive(true);
if (ray_alpha != null)
ray_alpha.SetActive(true);
}
if (Controller.UPvr_GetKeyDown(0, Pvr_KeyCode.APP)
|| Controller.UPvr_GetKeyDown(1, Pvr_KeyCode.APP)
)
{
SceneManager.LoadSceneAsync("Start");
}
RayCast();
}
/// <summary>
/// 设置主手柄
/// </summary>
/// <param name="hand"></param>
void SetMainHand(GameObject hand)
{
if (dot != null)
dot.SetActive(false);
if (rayLine != null)
rayLine.SetActive(false);
if (ray_alpha != null)
ray_alpha.SetActive(false);
currentController = hand;
dot = hand.transform.Find("dot").gameObject;
dot.SetActive(true);
rayLine = hand.transform.Find("ray_LengthAdaptive").gameObject;
rayLine.SetActive(true);
ray_alpha = hand.transform.Find("ray_alpha").gameObject;
ray_alpha.SetActive(true);
}
GameObject RayCastObj()
{
if (Controller.UPvr_GetMainHandNess() == 0)
{
if (Controller.UPvr_GetKeyDown(0, Pvr_KeyCode.TRIGGER) || Input.GetMouseButtonDown(0))
{
return RayCast();
}
else
return null;
}
else if (Controller.UPvr_GetMainHandNess() == 1)
{
if (Controller.UPvr_GetKeyDown(1, Pvr_KeyCode.TRIGGER) || Input.GetMouseButtonDown(0))
{
return RayCast();
}
else
return null;
}
else
return null;
}
/// <summary>
/// 物体是否点击
/// </summary>
/// <param name="go"></param>
/// <returns></returns>
public bool SelectObj(GameObject go)
{
if (RayCastObj() == go)
return true;
else
return false;
}
/// <summary>
/// 拿取
/// </summary>
/// <param name="go"></param>
public void TakeUpObj(GameObject go)
{
if (Controller.UPvr_GetMainHandNess() == 0)
{
if (Controller.UPvr_GetKeyDown(0, Pvr_KeyCode.Left) && RayCast() == go)
{
go.transform.SetParent(currentController.transform);
}
if (Controller.UPvr_GetKeyUp(0, Pvr_KeyCode.Left) && RayCast() == go)
{
go.transform.SetParent(null);
}
}
if (Controller.UPvr_GetMainHandNess() == 1)
{
if (Controller.UPvr_GetKeyDown(1, Pvr_KeyCode.Right) && RayCast() == go)
{
go.transform.SetParent(currentController.transform);
}
if (Controller.UPvr_GetKeyUp(1, Pvr_KeyCode.Right) && RayCast() == go)
{
go.transform.SetParent(null);
}
}
}
/// <summary>
/// 射线绘制
/// </summary>
/// <returns></returns>
GameObject RayCast()
{
Ray ray = new Ray(currentController.transform.position, currentController.transform.forward - currentController.transform.up * 0.25f);
RaycastHit hit;
rayLine.GetComponent<LineRenderer>().SetPosition(0, currentController.transform.TransformPoint(0, 0, 0.072f));
rayLine.GetComponent<LineRenderer>().SetPosition(1, dot.transform.position);
if (Physics.Raycast(ray, out hit, Mathf.Infinity))
{
// Debug.Log("碰撞对象: " + hit.collider.name);
Debug.DrawLine(ray.origin, hit.point, Color.red);
dot.transform.position = hit.point;
return hit.collider.gameObject;
}
else
return null;
}
}