C# 如何旋转游戏对象?
我正在看unity教程,在飞船的控制代码中,我想让它绕轴旋转,也就是说,它可以旋转360度例如,当我按下右键时,连续度 播放控制器:C# 如何旋转游戏对象?,c#,unity3d,rotation,unity5,gamecontroller,C#,Unity3d,Rotation,Unity5,Gamecontroller,我正在看unity教程,在飞船的控制代码中,我想让它绕轴旋转,也就是说,它可以旋转360度例如,当我按下右键时,连续度 播放控制器: using System.Collections; using System.Collections.Generic; using UnityEngine; [System.Serializable] public class Boundary { public float xMin, xMax, zMin, zMax; } public clas
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);
}
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
[系统可序列化]
公共阶级界限
{
公共浮点数xMin,xMax,zMin,zMax;
}
公共类玩家控制器:单行为{
[标题(“移动”)]
公众浮标速度;
公众浮标倾斜;
公共边界;
私人刚体钻机;
[标题(“射击”)]
公众游戏物体射击;
公共产卵;
公共浮式灭火器;
私人浮动nextFire;
无效唤醒(){
rig=GetComponent();
}
无效更新(){
if(Input.GetButton(“Fire1”)&&Time.Time>nextFire){
nextFire=时间。时间+燃速;
实例化(shot、shotpawn.position、Quaternion.identity);
}
}
无效固定更新(){
float moveHorizontal=Input.GetAxis(“水平”);
float moveVertical=Input.GetAxis(“垂直”);
Vector3移动=新的Vector3(水平移动,0f,垂直移动);
钻机速度=移动速度*速度;
rig.position=新矢量3(Mathf.Clamp(rig.position.x,boundary.xMin,boundary.xMax),0f,Mathf.Clamp(rig.position.z,boundary.zMin,boundary.zMax));
rig.rotation=四元数Euler(0f,0f,rig.velocity.x*-倾斜);
}
}
我如何编辑它来做我想要的
例如:
您可以使用
您的代码如下所示,使用您提供的示例:
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);
}
}
为了加快旋转速度,只需将矢量3.right与某个值相乘即可
若要绕另一轴旋转,请使用其他矢量方向,如Vector3.up。
例如:
transform.Rotate(Vector3.Up * moveHorizontal * Time.deltaTime);
transform.Rotate(Vector3.Forward * moveHorizontal * Time.deltaTime);
当你将向量3.Right与水平移动相乘时,当你按下“left键”时,向量3.Right也会起作用,这会导致向另一个方向旋转
transform.Rotate(Vector3.right * moveHorizontal * Time.deltaTime);
笔记:
只有当你的PlayerController连接到你的飞船游戏对象时,这才有效。如果它没有连接,你必须使用你的船的转换当然
世界空间与局部空间
在场景视图中单击对象时,应该会看到变换和指向不同方向(3轴)的3个箭头(红、绿、蓝)。使用上述参数的方法时,使用这些箭头作为旋转轴
也可以围绕世界空间轴旋转
transform.Rotate(Vector3.up, Time.deltaTime, Space.World);
何时使用transform.Rotate?
更改变换的位置或旋转时,使用变换
方法,它将应用于帧的末尾。这些变化忽略了物理学
->如果不关心碰撞,请使用transforms方法
何时使用rigidbody.MoveRotation?
当您更改rigidbody.position或使用rigidbodies方法时,它将在下一个物理步骤结束时应用。这些变化与物理有关(碰撞和其他)
->如果您关心碰撞,请使用刚体方法
谢谢你的提示
第三种可能性:
与直接调用transform.Rotate或rigidbody.MoveRotation不同,您还可以使用动画旋转对象,从而更改变换旋转
public Vector3 eulerAngleVelocity = new Vector3(0f,0f,1000f);
Transform.Rotate()的示例
您可以清楚地看到,当该对象在地面上旋转时,碰撞检查被忽略。(我把那张gif放在扰流板里以减少噪音。如果你想看的话,你需要把鼠标悬停在上面)
你可以用
您的代码如下所示,使用您提供的示例:
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);
}
}
为了加快旋转速度,只需将矢量3.right与某个值相乘即可
若要绕另一轴旋转,请使用其他矢量方向,如Vector3.up。
例如:
transform.Rotate(Vector3.Up * moveHorizontal * Time.deltaTime);
transform.Rotate(Vector3.Forward * moveHorizontal * Time.deltaTime);
当你将向量3.Right与水平移动相乘时,当你按下“left键”时,向量3.Right也会起作用,这会导致向另一个方向旋转
transform.Rotate(Vector3.right * moveHorizontal * Time.deltaTime);
笔记:
只有当你的PlayerController连接到你的飞船游戏对象时,这才有效。如果它没有连接,你必须使用你的船的转换当然
世界空间与局部空间
在场景视图中单击对象时,应该会看到变换和指向不同方向(3轴)的3个箭头(红、绿、蓝)。使用上述参数的方法时,使用这些箭头作为旋转轴
也可以围绕世界空间轴旋转
transform.Rotate(Vector3.up, Time.deltaTime, Space.World);
何时使用transform.Rotate?
更改变换的位置或旋转时,使用变换
方法,它将应用于帧的末尾。这些变化忽略了物理学
->如果不关心碰撞,请使用transforms方法
何时使用rigidbody.MoveRotation?
当您更改rigidbody.position或使用rigidbodies方法时,它将在下一个物理步骤结束时应用。这些变化与物理有关(碰撞和其他)
->如果您关心碰撞,请使用刚体方法
谢谢你的提示
第三种可能性:
与直接调用transform.Rotate或rigidbody.MoveRotation不同,您还可以使用动画旋转对象,从而更改变换旋转
public Vector3 eulerAngleVelocity = new Vector3(0f,0f,1000f);
Transform.Rotate()的示例
您可以清楚地看到,当该对象在地面上旋转时,碰撞检查被忽略。(我把那张gif放在扰流板里以减少噪音。如果你想看的话,你需要把鼠标悬停在上面)
这将是你旋转的速度和轴
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来获得所需的速度。希望这有帮助 这将是您旋转的速度和轴
public Vector3 eulerAngleVelocity = new Vector3(0f,0f,1000f);
由于要旋转刚体,所以请使用MoveRota