2014-12-18 5 views
0

현재 Unity를 사용하여 도약 모션 게임을하고 있습니다. 키보드를 사용하는 전통적인 1 인칭 컨트롤을 대체하기 위해 손짓을 사용합니다. 도약 모션 컨트롤러로 벽을 걸을 수 있습니다. 하지만 키보드를 사용하면 벽에 막 꽂힐 것입니다. 벽에 충돌 상자를 추가했습니다. 하지만 여전히 문제를 해결할 수는 없습니다.도약 모션 1 인칭 컨트롤러

다음은 당신이 직접 충돌 감지 어떠한 형태없이 위치를 변환 수정하는

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

http://unity3d.com/earn/tutorials/projects/space-shooter/moving-the-player 당신이 당신의 범인의 강체가 있습니까 : http://docs.unity3d.com/ScriptReference/Rigidbody.MovePosition.html

와는 어떻게 여기 강체를 통해 운동으로 보이는 학습 사이트에 VID 할 수있다 편집기에서 설정하고 사용 하시겠습니까? – Dinal24

답변

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

C#에서 내 도약 문자 컨트롤러 스크립트입니다. 화합의 충돌 자 및 물리 엔진을 사용하려면 이동을 위해 강체를 사용해야합니다. 또는 변형의 위치를 ​​직접 설정하려는 경우 자신의 충돌 감지 코드 (예 : 레이 캐스트 또는 일부)를 구현해야합니다.

여기 유니티 문서에 포함이 문제 :

+0

당신의 솔루션은 제게 많은 도움이됩니다. 고맙습니다. 늦게 고마워서 죄송합니다 !!! –

관련 문제