使用ConstantForce来模拟风

时间:2014-01-28 17:31:32

标签: c# unity3d 3d game-physics

我正在尝试创建一个简单的平台游戏。如果玩家发生碰撞,我会设置触发器,在玩家上启用ConstantForce,并按照选择的方向推动玩家(取决于触发器)。这对于向左和向右推动玩家效果相当好,但是我遇到问题的时候是玩家被推到Y轴上。玩家稍稍徘徊,但每次下降时,他都会开始越来越低 - 最终退出触发器。我希望他在空中保持一定的高度,除非他退出扳机。增加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;

}

}

1 个答案:

答案 0 :(得分:0)

Hacky方式

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;

}

另一种方法是使用重力值,直到感觉正确。您可以通过进入编辑 - &gt;来完成此操作项目设置 - &gt;物理