首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何旋转GameObject?

如何旋转GameObject?
EN

Stack Overflow用户
提问于 2018-03-04 11:42:01
回答 2查看 2.7K关注 0票数 1

我正在看unity教程,在飞船的控制代码中,我想让它在轴上旋转,也就是说,当我按右键的时候,它可以连续旋转360度​​度。

playerController:

代码语言:javascript
复制
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);
    }
}

我如何编辑它来做我想做的事情?

示例:

EN

回答 2

Stack Overflow用户

发布于 2018-03-04 12:45:47

您可以使用Transform.Rotate()

代码语言:javascript
复制
  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.up。例如:

代码语言:javascript
复制
transform.Rotate(Vector3.Up * moveHorizontal * Time.deltaTime);
transform.Rotate(Vector3.Forward * moveHorizontal * Time.deltaTime);

当你用你的moveHorizontal乘以Vector3.Right时,当你按下"left“键时,它也应该起作用,这应该会导致向另一个方向旋转。

代码语言:javascript
复制
 transform.Rotate(Vector3.right * moveHorizontal * Time.deltaTime);

备注:

只有当你的PlayerController被附加到你的飞船游戏对象上时才有效。如果它没有附加,当然你必须使用你的船的变换。

世界空间vs局部空间

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

也可以围绕WorldSpace轴旋转。

代码语言:javascript
复制
transform.Rotate(Vector3.up, Time.deltaTime, Space.World);

什么时候使用transform.Rotate?

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

什么时候使用rigidbody.MoveRotation?

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

如果你关心碰撞,->使用rigidbodies方法

感谢Helium的提示。

第三种可能性:

您也可以使用动画旋转对象,而不是直接调用transform.Rotate或rigidbody.MoveRotation,这会更改变换旋转。

Transform.Rotate()示例

你可以清楚地看到,当物体在地面上旋转时,碰撞检查被忽略了。

票数 3
EN

Stack Overflow用户

发布于 2018-03-04 15:22:20

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

代码语言:javascript
复制
public Vector3 eulerAngleVelocity = new Vector3(0f,0f,1000f);

因为您想旋转刚体,所以使用MoveRotation

代码语言:javascript
复制
Quaternion deltaRotation = Quaternion.Euler(-moveHorizontal * eulerAngleVelocity * Time.deltaTime);
rig.MoveRotation(rig.rotation * deltaRotation);

最终的代码将如下所示

代码语言:javascript
复制
    // 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来获得所需的速度。希望这能有所帮助。;)

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49091747

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档