Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何旋转量子并计算单位方向向量(ECS)_C#_Unity3d - Fatal编程技术网

C# 如何旋转量子并计算单位方向向量(ECS)

C# 如何旋转量子并计算单位方向向量(ECS),c#,unity3d,C#,Unity3d,我开始使用ECS,我有一个旋转组件四元数和一个平移组件float3。我正在编写一个系统,将处理用户输入和旋转和/或向前移动播放器 问题是我不知道怎么做。我认为现有的API是缺乏的 // Before it was easy if (Input.GetKey(KeyCode.W)) var newPostion = new Vector3(player.Position.x, player.Position.y, 0.3f) + _playerTra

我开始使用ECS,我有一个旋转组件四元数和一个平移组件float3。我正在编写一个系统,将处理用户输入和旋转和/或向前移动播放器

问题是我不知道怎么做。我认为现有的API是缺乏的

// Before it was easy
if (Input.GetKey(KeyCode.W))
    var newPostion = new Vector3(player.Position.x, player.Position.y, 0.3f)
                    + _playerTransform.up * Time.deltaTime * PlayerSpeed;

if (Input.GetKey(KeyCode.A))
    _playerTransform.Rotate(new Vector3(0, 0, PlayerRotationFactor));

// now it is not - that's part of the system's code
Entities.With(query).ForEach((Entity entity, ref Translation translation, ref Rotation rotation) =>
{
    float3 tmpTranslation = translation.Value;
    quaternion tmpRotation = rotation.Value;

    if (Input.GetKey(KeyCode.W))
        // how to retrieve forward vector from the quaternion
        tmpTranslation.y += Time.deltaTime * PlayerSpeed; 

    if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
        // this always set the same angle
        tmpRotation = quaternion.RotateZ(PlayerRotationFactor); 

    // ECS need this
    var newTranslation = new Translation { Value = tmpTranslation };
    PostUpdateCommands.SetComponent(entity, newTranslation);
    var newRotation = new Rotation { Value = tmpRotation };
    PostUpdateCommands.SetComponent(entity, newRotation);
});

要从四元数获取向量,只需将其与正向向量相乘,例如:

如果输入.getKeyCode.W { 矢量前进=t保护*浮动30,0,1; tmptransation+=前进*时间.延迟*播放速度; } 对于旋转部分,如您所说//始终设置相同的角度。看起来你正在创建一个新的静态角度。尝试更改以与当前角度相乘,就像与要合并的四元数相乘一样。例如:

如果输入.GetKeyCode.A | |输入.GetKeyCode.LeftArrow { t保护=多重保护,四元数.RotateZPlayerRotationFactor; }
旁注:我对Unity ECS也有点陌生,我还没有测试过以上代码。

您是一位数学天才先生!!答案是100%正确的。下面的代码经过调整以匹配现有API的命名。很高兴我能提供帮助:
// correct forward
if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow))
{
    float3 forwardVector = math.mul(rotation.Value, new float3(0, 1, 0));
    newTranslation += forwardVector * Time.deltaTime * PlayerSpeed;
}
// correct rotation
if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
{
    quaternion newRotation = math.mul(rotation.Value, quaternion.RotateZ(PlayerRotationFactor * Time.deltaTime));
    // insert new value to the component
    PostUpdateCommands.SetComponent(entity, new Rotation { Value = newRotation });
}
// thx akaJag