2014-01-28 71 views
0

我正在嘗試創建一個簡單的平臺。我在觸發器周圍設置瞭如果玩家碰撞的地方,玩家可以啓用ConstantForce,玩家可以按照選定的方向(取決於觸發器)進行推動。這在將玩家向左和向右推動的情況下工作得相當好,但是我遇到問題的時候是玩家在Y軸上被推上去的時候。玩家徘徊一段時間,但每次他下降,他開始越來越低 - 最終退出觸發器。除非他退出觸發器,否則我希望他在空中保持一定的高度。增加ConstantForce只會讓他飛得更高,減少他的質量也有類似的效果。使用ConstantForce模擬風

private ConstantForce getConstantForce; 
public GameObject findingThePlayer; 
public Vector3 forceDirection = new Vector3(); 


void Start() 

{ 
    //Getting the ConstantForce component from player 
    findingThePlayer = GameObject.Find("pig_test"); 
    getConstantForce = findingThePlayer.GetComponent<ConstantForce>(); 


} 



void OnTriggerStay() 

{ 



    //turning on the players ConstantForce 
    getConstantForce.enabled = true; 

    //we set the vector value in editor, depending which way we want the wind to blow 
    getConstantForce.constantForce.force = forceDirection; 




} 


void OnTriggerExit() 

{ 
    //When player exits trigger, ConstantForce is disabled 
    getConstantForce.enabled = false; 

} 

}

回答

0

哈克的方式

void OnTriggerStay() 
{ 

    //turning on the players ConstantForce 
    getConstantForce.enabled = true; 

    //we set the vector value in editor, depending which way we want the wind to blow 
    getConstantForce.constantForce.force = forceDirection; 

    //Once you reach the height you want, set the body to be kinematic, 
    // that way forces will not longer have any effect 

    findingThePlayer.rigidbody.isKinematic = true; 
} 

void OnTriggerExit() 
{ 
    //When player exits trigger, ConstantForce is disabled 
    getConstantForce.enabled = false; 

    //And lets make it a physics rigidbody again on exit 
    findingThePlayer.rigidbody.isKinematic = false; 

} 

另一種方法是,直到感覺對與重力值的發揮。你可以進入編輯 - >項目設置 - >物理