简体   繁体   English

跳跃运动第一人称控制器

[英]leap motion first person controller

I am currently working on a leap motion game using Unity. 我目前正在使用Unity开发跳动作游戏。 When I just to use the hand gesture to replace the traditional first person control which used the keyboard. 当我只是使用手势来代替使用键盘的传统第一人称控制时。 I am able to walking through the wall with the leap motion controller. 我能够使用跳跃运动控制器在墙上穿行。 But when i use keyboard, it will just stop at the wall. 但是当我使用键盘时,它只会停在墙上。 I have added a collision box to the wall. 我在墙上加了一个防撞箱。 But still i cannot fix the problems. 但我仍然无法解决问题。

Below is my Leap Characters Controller script in C# 以下是我在C#中的Leap Characters Controller脚本

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;
        }
    }

You are directly modifying the transform's position without any form of collision detection. 您将直接修改变换的位置,而无需任何形式的碰撞检测。 If you want to use unity's colliders and physics engine you need to use the rigidbody for movement or if you want to stick with directly setting the transform's position you need to implement your own collision detection code (maybe raycasts or some such). 如果要使用单位的对撞机和物理引擎,则需要使用刚体进行运动,或者如果要坚持直接设置变换的位置,则需要实现自己的碰撞检测代码(可能是射线广播或类似的代码)。

This issue covered in the unity docs here: http://docs.unity3d.com/ScriptReference/Rigidbody.MovePosition.html 此问题在此处的统一文档中介绍: http : //docs.unity3d.com/ScriptReference/Rigidbody.MovePosition.html

and there is a how to vid on the learn site which looks into the movement via rigidbody here: http://unity3d.com/earn/tutorials/projects/space-shooter/moving-the-player 在学习站点上有一个如何通过硬体查看运动的视频: http//unity3d.com/earn/tutorials/projects/space-shooter/moving-the-player

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM