我对此并不陌生,正在尝试学习如何使用C#和unity。当您在x轴上来回移动时,我目前正在尝试倾斜我的船。但是我遇到了编译器错误,但是看不到?任何帮助,将不胜感激 :)
错误是这样的:
资产/脚本/PlayerController.cs(28,62):错误CS0029:无法将类型'UnityEngine.Quaternion'隐式转换为'float'
using UnityEngine;
using System.Collections;
[System.Serializable]
public class Boundary {
public float xMin, xMax, yMin, yMax;
}
public class PlayerController : MonoBehaviour {
public float speed;
public float tilt;
public Boundary boundary;
void FixedUpdate () {
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector2 movement = new Vector2 (moveHorizontal, moveVertical);
this.gameObject.GetComponent<Rigidbody2D> ().velocity = movement * speed;
this.gameObject.GetComponent<Rigidbody2D> ().position = new Vector2
(
Mathf.Clamp (this.gameObject.GetComponent<Rigidbody2D> ().position.x, boundary.xMin, boundary.xMax),
Mathf.Clamp (this.gameObject.GetComponent<Rigidbody2D> ().position.y, boundary.yMin, boundary.yMax)
);
//issue is on this line
this.gameObject.GetComponent<Rigidbody2D> ().rotation = Quaternion.Euler (0.0f, this.gameObject.GetComponent<Rigidbody2D> ().velocity.x * -tilt, 0.0f);
}
}
最佳答案
当您使用Rigidbody
时,您正在使用的教程可能使用Rigidbody2D
(3D)。Rigidbody2D.rotation
是围绕Z轴的float
旋转。Rigidbody.rotation
是Quaternion
3d旋转。您的代码创建了一个Quaternion
,它将与Rigidbody
一起使用,但是您有一个Rigidbody2D
。
有两种方法可以修复该错误:
使用2d旋转,跳过Quaternion
的创建:
var rigidbody2D = GetComponent<Rigidbody2D>();
rigidbody2D.rotation = rigidbody2D.velocity.x * -tilt;
更改您的代码和Unity对象以使用
Rigidbody
。关于c# - 统一的bodybody2d倾斜问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33836768/