2014-09-01 79 views
0

我有一個場景,玩家可以拾取和丟棄物體,以及移動和環視。這個奇怪的抖動爲什麼會發生?

所有的球員對象是一個空的遊戲對象「MainCharacter」的孩子:

MainCharacter > 
    Capsule (With RigidBody and PlayerMoveScript) > 
     PlayerBase (empty - used for checking if grounded) 
     MainCamera > 
      Hands(With PickUpDrop script) 

我拿起Lerps我的手的位置,目的不過我的膠囊與任何牆壁相撞後有一個奇怪的抖動,其我無法弄清楚如何修復!

繼承人的.exe文件:GameTest 繼承人的數據文件夾:Data

下面是腳本:

拿起並刪除腳本:

public bool handsFull = false; 

    public float distanceMax = 20f; 

    public Transform handPosition; 

    public LayerMask canPickUp; 

    public GameObject taggedGameObject; 

    public bool colliderTriggered; 

    public bool bounds; 

    public PickedUpObject pickedUpScript; 

    public Rigidbody player; 

    // Use this for initialization 
    void Start() { 

     print(FindClosestPickup().name); 
     handPosition = transform; 
     pickedUpScript = null; 

    } 

    // Update is called once per frame 
    void Update() { 


       if (Input.GetKeyDown (KeyCode.E) && !bounds) { 
         if (Physics.CheckSphere (handPosition.position, 2f, canPickUp)) { 

           if (handsFull) { 
             Drop(); 
           } 

           if (!handsFull) { 
             PickedUp(); 
           } 

       handsFull = !handsFull; 

         } 
       } 

       if (handsFull) { 
         RotateMovePickedUpObject(); 
       } 


     } 



    private void PickedUp(){ 

     //Closest object to top of list 
     taggedGameObject = (GameObject)FindClosestPickup(); 

     taggedGameObject.collider.isTrigger = true; 



     taggedGameObject.rigidbody.useGravity = false; 
     taggedGameObject.rigidbody.isKinematic = true; 

     pickedUpScript = taggedGameObject.GetComponent<PickedUpObject>(); 

     Debug.Log ("Pick Up"); 


    } 

    private void RotateMovePickedUpObject(){ 

     //Rotate 

     if(Input.GetKeyDown(KeyCode.End)){ 
      taggedGameObject.transform.localRotation *= Quaternion.Euler(0, 0, 45); 
     } 
     if(Input.GetKeyDown(KeyCode.Delete)){ 
      taggedGameObject.transform.localRotation *= Quaternion.Euler(0, 45, 0); 
     } 
     if(Input.GetKeyDown(KeyCode.PageDown)){ 
      taggedGameObject.transform.localRotation *= Quaternion.Euler(0, -45, 0); 
     } 
     if(Input.GetKeyDown(KeyCode.Home)){ 
      taggedGameObject.transform.localRotation *= Quaternion.Euler(0, 0, -45); 
     } 
     if(Input.GetKeyDown(KeyCode.PageUp)){ 
      taggedGameObject.transform.localRotation *= Quaternion.Euler(-45, 0, 0); 
     } 
     if(Input.GetKeyDown(KeyCode.Insert)){ 
      taggedGameObject.transform.localRotation *= Quaternion.Euler(45, 0, 0); 
     } 

     taggedGameObject.transform.position = Vector3.Lerp(taggedGameObject.transform.position, handPosition.position, (1 - Mathf.Exp(-20 * Time.smoothDeltaTime)) * 10); 

    } 


    private void Drop(){ 

     taggedGameObject.collider.isTrigger = false; 

     taggedGameObject.rigidbody.useGravity = true; 
     taggedGameObject.rigidbody.isKinematic = false; 

     taggedGameObject = null; 

     Debug.Log ("Drop"); 

     pickedUpScript = null; 

    } 

    private GameObject FindClosestPickup() { 

     //Find closest gameobject with tag 
     GameObject[] gos; 
     gos = GameObject.FindGameObjectsWithTag("pickup"); 
     GameObject closest = null; 
     float distance = Mathf.Infinity; 
     Vector3 position = transform.position; 

     foreach (GameObject go in gos) { 
      Vector3 diff = go.transform.position - position; 
      float curDistance = diff.sqrMagnitude; 
      if (curDistance < distance) { 
       closest = go; 
       distance = curDistance; 
      } 
     } 

     return closest; 
    } 

} 

拾取對象腳本:

public PickUpDrop pickUpScript; 
    public GameObject thisOne; 
    public Color thecolor; 
    public bool inObject; 

    // Use this for initialization 
    void Start() { 
     thisOne = this.gameObject; 
    } 

    // Update is called once per frame 
    void Update() 
    { 
     thecolor = thisOne.renderer.material.color; 

    if (pickUpScript.taggedGameObject != thisOne) 
     { 
      gameObject.renderer.material.color = Color.gray; 
     } 

    if (pickUpScript.taggedGameObject == thisOne) 
     { 
      Color color = renderer.material.color; 
      color.a = 0.5f; 
      renderer.material.color = color; 
     } 
    } 

    void OnTriggerEnter() 
    { 
     if (thisOne == pickUpScript.taggedGameObject) 
     { 
      inObject = true; 
      pickUpScript.bounds = true; 
      gameObject.renderer.material.color = Color.red; 

     } 
    } 

    void OnTriggerExit() 
    { 
     if(thisOne == pickUpScript.taggedGameObject) 
     { 
      inObject = false; 
      pickUpScript.bounds = false; 
      gameObject.renderer.material.color = Color.gray;   
     } 
    } 

} 
+0

我還沒有調試你的代碼,但我注意到你有一個'GameObject FindClosestPickup()'方法。如果有多個對象完全關閉,那麼由於[浮點錯誤](http://floating-point-gui.de/errors/comparison/),該方法返回的對象可能會不穩定。由於該方法在每個遊戲更新上返回不同的對象,因此不穩定性可能導致翻轉。這可能是這種情況嗎? – dbc 2014-09-01 15:59:25

+0

我懷疑這是原因,我的物體距離彼此很遠,抖動仍然只發生在一個物體上,但是感謝您的未來警告! – user3902259 2014-09-01 20:12:41

回答

0
taggedGameObject.transform.position = Vector3.Lerp(taggedGameObject.transform.position, handPosition.position, (1 - Mathf.Exp(-20 * Time.smoothDeltaTime)) * 10); 

這條線將繼續向着手的位置移動物體。如果您正在移動遊戲物體的剛體,那麼在物理計算過程中作用於該物體的物理將與更新功能期間物體的手動移動發生衝突。

這取決於您在發生衝突時想要發生什麼情況。如果你只是想讓'抖動'停止並且仍然能夠將物體與其他物理物體對準,那就使用它;

taggedGameObject.rigidbody.AddForce((taggedGameObject.transform.position - handPosition.position) * force); 

這將保持與剛體的所有相互作用。您必須調整移動物體的力量,並且可能會在標記的遊戲對象處於玩家手中時禁用重力。但它應該有預期的效果。

+0

你好! 謝謝你的幫助,大部分作品的腳本! (我不得不扭轉計算(handPosition.position-taggameObject.transform.position)) 我已經暫時解決了彈簧關節的問題,但是這個修復讓我能夠以更有效的方式解決問題! 再次感謝! – user3902259 2014-09-02 11:33:04

相關問題