[英]Unity rigidbody2d tilt issue
I'm new to this and trying to learn how to use C# and unity. 我是新手,并试图学习如何使用C#和统一。 I'm currently trying to tilt my ship as you move back and forth on the x axis. 我正在尝试在x轴上来回移动时倾斜我的船。 however I'm getting a compiler error but cant see it? 但是我收到编译器错误但看不到它? any help would be appreciated :) 任何帮助,将不胜感激 :)
The error is this: 错误是这样的:
Assets/Scripts/PlayerController.cs(28,62): error CS0029: Cannot implicitly convert type 'UnityEngine.Quaternion' to 'float' Assets / Scripts / 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);
}
}
The tutorial you're using probably uses Rigidbody
(3D), while you're using Rigidbody2D
. 您正在使用的教程可能使用Rigidbody
(3D),而您正在使用Rigidbody2D
。
Rigidbody2D.rotation
is a float
rotation around the Z axis. Rigidbody2D.rotation
是围绕Z轴的float
旋转。
Rigidbody.rotation
is a Quaternion
3d rotation. Rigidbody.rotation
是一个Quaternion
3d旋转。 Your code creates a Quaternion
, which would work with Rigidbody
, but you have a Rigidbody2D
. 你的代码创建了一个Quaternion
,它可以与Rigidbody
,但你有一个Rigidbody2D
。
There are two ways to fix the error: 有两种方法可以解决错误:
Use a 2d rotation, skipping creation of the Quaternion
: 使用二维旋转,跳过Quaternion
创建:
var rigidbody2D = GetComponent<Rigidbody2D>(); rigidbody2D.rotation = rigidbody2D.velocity.x * -tilt;
Change your code and Unity objects to use Rigidbody
. 更改您的代码和Unity对象以使用Rigidbody
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.