在Unity中将玩家旋转从A点设置为B.

时间:2018-01-27 17:31:33

标签: c# unity3d

移动播放器时,我使用此代码进行播放器旋转。玩家应该始终旋转到他将移动到的目标点。

private void SetPlayerRotation(Vector3 targetCellPosition)
{
    Vector3 targetPoint = new Vector3(targetCellPosition.x, transform.position.y, targetCellPosition.z) - transform.position;

    transform.rotation = Quaternion.LookRotation(targetPoint, Vector3.up); // rotate the player
}

有时控制台会记录

  

查看旋转查看向量为零

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

如果vector为零,则会记录该值,因此if语句将修复此问题。记录这个的原因是当旋转矢量为0时没有任何反应,所以做这个任务是没有意义的。

<div class="form-check">
    <input class="form-check-input" type="checkbox" value="" onchange="javascript:addFilter(event,'freshman');">
    <label class="form-check-label" for="defaultCheck1">Freshman</label>
</div>
<script>
    function addFilter(element,string) {
      let filter_array =[];
      if(element.value){
      if(string =="freshman"){
        filter_array.push(string);
        console.log("Add Freshman to Array");
      }
      if(string =="sophmore"){
        filter_array.push(string);
        console.log("Add Junior to Array");
      }
      if(string =="junior"){
        filter_array.push(string);
        console.log("Add junior to Array");
      }
      if(string =="senior"){
        filter_array.push(string);
        console.log("Add Senior from to Array");
      }

    }

else {

      if(string =="freshman"){
        filter_array.push(string);
        console.log("Remove Freshman from Array");
        console.log(element);
      }
      if(string =="sophmore"){
        filter_array.push(string);
        console.log("Remove Junior from Array");
      }
      if(string =="junior"){
        filter_array.push(string);
        console.log("Remove junior from Array");
      }
      if(string =="senior"){
        filter_array.push(string);
        console.log("Remove Senior from Array");
      }
    }
}
  </script>