飞跃运动第一人称控制器
本文关键字:控制器 第一人称 运动 | 更新日期: 2023-09-27 18:34:29
我目前正在使用Unity开发一个跳跃运动游戏。当我只是用手势来取代使用键盘的传统第一人称控制时。我能够用跳跃运动控制器穿过墙壁。但是当我使用键盘时,它只会停在墙上。我在墙上加了一个防撞盒。但我仍然无法解决问题。
下面是我在 C# 中的 Leap 字符控制器脚本
using UnityEngine;
using System.Collections;
using Leap;
public class LeapCharacterController : MonoBehaviour {
Controller m_leapController;
float m_lastBlastTime = 0.0f;
GameObject m_carriedObject;
bool m_handOpenThisFrame = false;
bool m_handOpenLastFrame = false;
// Use this for initialization
void Start () {
m_leapController = new Controller();
}
// gets the hand furthest away from the user (closest to the screen).
Hand GetForeMostHand() {
Frame f = m_leapController.Frame();
Hand foremostHand = null;
float zMax = -float.MaxValue;
for(int i = 0; i < f.Hands.Count; ++i) {
float palmZ = f.Hands[i].PalmPosition.ToUnityScaled().z;
if (palmZ > zMax) {
zMax = palmZ;
foremostHand = f.Hands[i];
}
}
return foremostHand;
}
void OnHandOpen(Hand h) {
m_carriedObject = null;
}
void OnHandClose(Hand h) {
// look for an object to pick up.
RaycastHit hit;
if(Physics.SphereCast(new Ray(transform.position + transform.forward * 2.0f, transform.forward), 2.0f, out hit)) {
m_carriedObject = hit.collider.gameObject;
}
}
bool IsHandOpen(Hand h) {
return h.Fingers.Count > 1;
}
// processes character camera look based on hand position.
void ProcessLook(Hand hand) {
float handX = hand.PalmPosition.ToUnityScaled().x;
transform.RotateAround(Vector3.up, handX * 0.30f);
}
void MoveCharacter(Hand hand) {
if (hand.PalmPosition.ToUnityScaled().z > 0) {
transform.position += transform.forward * 0.1f;
}
if (hand.PalmPosition.ToUnityScaled().z < -1.0f) {
transform.position -= transform.forward * 0.04f;
}
}
// Determines if any of the hand open/close functions should be called.
void HandCallbacks(Hand h) {
if (m_handOpenThisFrame && m_handOpenLastFrame == false) {
OnHandOpen(h);
}
if (m_handOpenThisFrame == false && m_handOpenLastFrame == true) {
OnHandClose(h);
}
}
// if we're carrying an object, perform the logic needed to move the object
// with us as we walk (or pull it toward us if it's far away).
void MoveCarriedObject() {
if (m_carriedObject != null) {
Vector3 targetPos = transform.position + new Vector3(transform.forward.x, 0, transform.forward.z) * 5.0f;
Vector3 deltaVec = targetPos - m_carriedObject.transform.position;
if (deltaVec.magnitude > 0.1f) {
m_carriedObject.rigidbody.velocity = (deltaVec) * 10.0f;
} else {
m_carriedObject.rigidbody.velocity = Vector3.zero;
}
}
}
void FixedUpdate () {
Hand foremostHand = GetForeMostHand();
if (foremostHand != null) {
m_handOpenThisFrame = IsHandOpen(foremostHand);
ProcessLook(foremostHand);
MoveCharacter(foremostHand);
HandCallbacks(foremostHand);
MoveCarriedObject();
}
m_handOpenLastFrame = m_handOpenThisFrame;
}
}
void MoveCharacter(Hand hand) {
if (hand.PalmPosition.ToUnityScaled().z > 0) {
transform.position += transform.forward * 0.1f;
}
if (hand.PalmPosition.ToUnityScaled().z < -1.0f) {
transform.position -= transform.forward * 0.04f;
}
}
您可以直接修改变换的位置,而无需任何形式的冲突检测。如果你想使用unity的碰撞体和物理引擎,你需要使用刚体进行移动,或者如果你想坚持直接设置变换的位置,你需要实现你自己的碰撞检测代码(可能是光线投射或类似的东西(。
此处的 unity 文档中涵盖了此问题:http://docs.unity3d.com/ScriptReference/Rigidbody.MovePosition.html
在学习网站上有一个如何视频,它通过刚体查看运动:http://unity3d.com/earn/tutorials/projects/space-shooter/moving-the-player