1

我在看unity教程,在船的控制代码里我想让它绕轴旋转,也就是可以360度连续旋转,比如我按右键。

播放器控制器:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[System.Serializable]
public class Boundary
{
    public float xMin, xMax, zMin, zMax;
}

public class PlayerController : MonoBehaviour {

    [Header("Movement")]
    public float speed;
    public float tilt;
    public Boundary boundary;
    private Rigidbody rig;

    [Header("Shooting")]
    public GameObject shot;
    public Transform shotSpawn;
    public float fireRate;
    private float nextFire;

    void Awake () {
        rig = GetComponent<Rigidbody>();
    }

    void Update () {
        if (Input.GetButton ("Fire1") && Time.time > nextFire) {
            nextFire = Time.time + fireRate;
            Instantiate (shot, shotSpawn.position, Quaternion.identity);
        }
    }

    void FixedUpdate () {
        float moveHorizontal = Input.GetAxis ("Horizontal");
        float moveVertical = Input.GetAxis ("Vertical");
        Vector3 movement = new Vector3(moveHorizontal, 0f, moveVertical);
        rig.velocity = movement * speed;
        rig.position = new Vector3 (Mathf.Clamp (rig.position.x, boundary.xMin, boundary.xMax), 0f, Mathf.Clamp (rig.position.z, boundary.zMin, boundary.zMax));
        rig.rotation = Quaternion.Euler (0f, 0f, rig.velocity.x * -tilt);
    }
}

我怎样才能编辑它做我想做的事?

例子:

在此处输入图像描述

4

2 回答 2

3

您可以使用Transform.Rotate()

您提供的示例代码如下所示:

  void FixedUpdate () {
    float moveHorizontal = Input.GetAxis ("Horizontal");
    float moveVertical = Input.GetAxis ("Vertical");
    Vector3 movement = new Vector3(moveHorizontal, 0f, moveVertical);
    rig.velocity = movement * speed;
    rig.position = new Vector3 (Mathf.Clamp (rig.position.x, boundary.xMin, boundary.xMax), 0f, Mathf.Clamp (rig.position.z, boundary.zMin, boundary.zMax));
    if(moveHorizontal > 0){ //if your "right key" is pressed
      // Rotate the object around its local X axis at 1 degree per second
      transform.Rotate(Vector3.right * Time.deltaTime);
    }
}

为了更快的旋转,您可以简单地将 Vector3.right 与某个值相乘。

要围绕另一个轴旋转,请使用其他矢量方向,例如 Vector3.up。例如:

transform.Rotate(Vector3.Up * moveHorizontal * Time.deltaTime);
transform.Rotate(Vector3.Forward * moveHorizontal * Time.deltaTime);

当您将 Vector3.Right 与 moveHorizo​​ntal 相乘时,当您按下“左键”时它也应该可以工作,这应该会导致向另一个方向旋转。

 transform.Rotate(Vector3.right * moveHorizontal * Time.deltaTime);

笔记:

这仅在您的 PlayerController 附加到您的船游戏对象时才有效。如果它没有连接,你当然必须使用你的船的变换。

世界空间与局部空间

当您在场景视图中单击对象时,您应该会看到变换和指向不同方向(3 轴)的 3 个箭头(红色、绿色、蓝色)。当您使用带有上述参数的方法时,您使用这些箭头作为旋转轴。

您还可以围绕 WorldSpace 轴旋转。

transform.Rotate(Vector3.up, Time.deltaTime, Space.World);

何时使用 transform.Rotate?

当您使用 transforms 方法更改变换的位置或旋转时,它将应用于帧的末尾。这些变化忽略了物理学。

-> 如果您不关心碰撞,请使用变换方法

何时使用刚体.MoveRotation?

当您使用刚体方法更改刚体.position 或刚体.MoveRotation 时,它将在下一个物理步骤结束时应用。这些变化关心物理(碰撞和东西)

-> 如果您关心碰撞,请使用刚体方法

感谢Helium的提示。

第三种可能:

除了直接调用transform.Rotate 或rigidbody.MoveRotation,您还可以使用动画来旋转对象,这会改变变换旋转。

Transform.Rotate() 示例

您可以清楚地看到,该对象在地面旋转时会忽略碰撞检查。(我将那个 gif 打包到扰流板中以减少噪音。如果你想看到它,你需要将鼠标悬停在它上面)

显示变换旋转的 gif

于 2018-03-04T04:45:46.690 回答
2

这将是您旋转的速度和轴。

public Vector3 eulerAngleVelocity = new Vector3(0f,0f,1000f);

由于您要旋转刚体,因此请使用 MoveRotation

Quaternion deltaRotation = Quaternion.Euler(-moveHorizontal * eulerAngleVelocity * Time.deltaTime);
rig.MoveRotation(rig.rotation * deltaRotation);

最终代码将如下所示

    // NEW CODE BEGIN------
public Vector3 eulerAngleVelocity = new Vector3(0f,0f,1000f);
// NEW CODE END------

void FixedUpdate () {
    float moveHorizontal = Input.GetAxis ("Horizontal");
    float moveVertical = Input.GetAxis ("Vertical");
// NEW CODE BEGIN------
    Vector3 movement = new Vector3(0f, 0f, moveVertical); // Notice I have removed moveHorizontal, this will make sure your gameobject doesnt go left and right. We will use move horizontal for rotating the gameobject.
// NEW CODE END------
    rig.velocity = movement * speed;
    rig.position = new Vector3 (Mathf.Clamp (rig.position.x, boundary.xMin, boundary.xMax), 0f, Mathf.Clamp (rig.position.z, boundary.zMin, boundary.zMax));
    // NEW CODE BEGIN------
    Quaternion deltaRotation = Quaternion.Euler(-moveHorizontal * eulerAngleVelocity * Time.deltaTime);
    rig.MoveRotation(rig.rotation * deltaRotation);
    // NEW CODE END------
}

您可以使用 eulerAngleVelocity 来获得所需的速度。希望这可以帮助。;)

于 2018-03-04T07:22:20.047 回答