在将力施加到GameObject后取最终位置

时间:2015-01-28 15:11:59

标签: unity3d

我对游戏对象施加一个力量,它按预期运行。当它的速度变为零时,我想采取这个游戏对象的最终位置。我使用像这样的算法

     void Shoot ()
 {
 ...
    if(Mouse button clicked)
     {
     gameobject.rigidbody.AddForce;
     shoot = true;
     }
 }

 void Update ()
 {
    Shoot();
    if(gameobject.rigidbody.velocity.magnitude==0 && shoot==true)
    {
     finalPosition==gameobject.transform.position;
     shoot = false;
    }
 }

然而,对于我刚拍摄的第一个即时帧(施加力但速度不会立即改变),速度== 0并且拍摄变为真。这就是为什么我采取起始位置而不是最终位置的原因。但是,我需要在游戏对象的位置变为零速度之后取得它的位置你能告诉我一个方法吗?

2 个答案:

答案 0 :(得分:0)

使用int而不是bool进行拍摄并将其设置为0.当您进行拍摄时将其设置为1,然后在更新中检测到它大于0时检测是否为真。当幅度为0且拍摄为1时,进行拍摄2,然后当幅度为0且拍摄为2时,记录位置并将拍摄设置为0.注意:您可能需要将其移动到FixedUpdate方法中,以便它只发生在与物理计算的速率相同。示例如下:

private void Update()
{
    if (Input.GetMouseButtonDown(0))
    {
         //add you force
         shoot = 1;
    }
}

private void FixedUpdate()
{
    Shoot();
    if(shoot > 0 && gameobject.rigidbody.velocity.magnitude==0)
    {
        if (shoot == 1)
        {
            shoot = 2;
        }
        else
        {
            finalPosition==gameobject.transform.position;
            shoot = 0;
        }
    }
}

答案 1 :(得分:0)

感谢下面的代码,您可以在(0,0.5)

之间通知幅度
 void Shoot (){
    ...
    if(Mouse button clicked){
      gameobject.rigidbody.AddForce;
    }
 }

 void Update (){
    // First check shoot paramater for more performance
    if(shoot == false && ((gameobject.rigidbody.velocity.magnitude>0)
                      &&(gameobject.rigidbody.velocity.magnitude<=0.5))){ 
       Shoot();  //object start moving so do some job.
       shoot = true;  // the object is moving
       // now you can know your object has movement 
       // and also know  <=0.5 as you wish in comment.
    }else if(gameobject.rigidbody.velocity.magnitude==0 && shoot==true){
       finalPosition==gameobject.transform.position;
       shoot = false;
    }
 }

我认为下面是更好的解决方案。

首先在你的类或单独的类中创建枚举,以获得更恰当和可读的代码。

public enum MovementState {
   Initial = 0,
   FirstMove = 1,
   Moving = 2,
   Stop = 3
}

在课堂上,您可以在下面添加。

public MovementState objectState = MovementState.Initial; //you can see this and changes in editor

 void Shoot (){
    ...
    if(Mouse button clicked){
      objectState = MovementState.Initial;
      gameobject.rigidbody.AddForce;
    }
 }

 void Update (){
    // First check shoot paramater for more performance
    if(objectState == MovementState.Initial && ((gameobject.rigidbody.velocity.magnitude>0)
                      &&(gameobject.rigidbody.velocity.magnitude<=0.5))){ 
       Shoot();  //object start moving so do some job.
       objectState = MovementState.FirstMove;
       // now you can know your object has movement 
       // and also know  <=0.5 as you wish in comment.
    }else if(objectState==MovementState.Moving && gameobject.rigidbody.velocity.magnitude==0){
       finalPosition==gameobject.transform.position;
       objectState = MovementState.Stop;
    }else if(objectState==MovementState.FirstMove){
       objectState = MovementState.Moving;
    }
 }

代码中可能存在一些语法错误。我不检查它。