gpt4 book ai didi

c# - Unity 3D - 没有旋转轴的旋转游戏对象

转载 作者:太空宇宙 更新时间:2023-11-03 20:52:57 25 4
gpt4 key购买 nike

希望我的标题概括了我的问题。我在 2d 游戏中有一个火箭,它只能在屏幕上水平移动。我希望它朝着玩家的手指(移动方向)旋转,但无法找到一种方法来旋转对象而不旋转它移动的整个轴。我只需要它看起来像是已经转动了,但它应该继续沿着 x 移动。我该怎么做?

void Start () {
//scoreT = GetComponent<TextMeshProUGUI> ();
gameSpeed = 1;
score = 0;
Rigidbody2D rb2d = GetComponent<Rigidbody2D> ();
}

// Update is called once per frame
void Update () {
float MoveHorizontal = Input.GetAxis ("Horizontal");
Vector2 Movement = new Vector2 (MoveHorizontal, 0.0f);

rb2d.rotation = Quaternion.Euler (0.0f, 0, 0f, rb2d.velocity.x * -tilt);
transform.Translate (MoveHorizontal * speed, 0, 0);

最佳答案

您可以做的一件事是修改火箭的 rigidbody.rotation 使其在移动时向一个或另一个方向倾斜。例如:

float tilt - 0.3f;
//In case you prefer the rotation in another axis you just need to modify the position of the rigidbody.velocity.x * -tilt
rigidbody.rotation = Quaternion.Euler (0.0f, 0.0f, rigidbody.velocity.x * -tilt);

由于您没有添加任何代码,我不确定您是如何移动火箭的,所以我将发布一个通用代码,您需要根据自己的项目进行调整:

public class PlayerController : MonoBehaviour
{
public float speed;
//The tild factor
public float tilt;
//The limit within the spaceship can move
public Boundary boundary;

void FixedUpdate ()
{
//You will need to capture the screen touchs of the user for the inputs
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");

//Applying the movement to the GameObject
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rigidbody.velocity = movement * speed;

//To ensure the GameObject doesnt move outside of the game boundary
rigidbody.position = new Vector3
(
Mathf.Clamp (rigidbody.position.x, boundary.xMin, boundary.xMax),
0.0f,
Mathf.Clamp (rigidbody.position.z, boundary.zMin, boundary.zMax)
);

//Here is where you apply the rotation
rigidbody.rotation = Quaternion.Euler (0.0f, 0.0f, rigidbody.velocity.x * -tilt);
}
}

顺便说一句,您正在制作一款太空 2D 游戏,您可能有兴趣查看本教程:

https://unity3d.com/learn/tutorials/s/space-shooter-tutorial

关于c# - Unity 3D - 没有旋转轴的旋转游戏对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53408424/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com