Unity 3D - 没有旋转轴的旋转游戏对象 [英] Unity 3D - rotating gameobject without rotating axis

查看:79
本文介绍了Unity 3D - 没有旋转轴的旋转游戏对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Hope my title summarises my problem. I have a rocket on a 2d game that only moves horizontally across the screen. I want it to rotate towards the players finger (the direction of movement), but cannot find a way to rotate the object without rotating the whole axis it moves on. I simply need it to seem like it has turned, but it should keep moving along the x. How can I go about this?

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 使其在移动时倾斜到 1方向或另一个.例如:

One thing you can do is to modify rigidbody.rotation of your rocket rocket to make it tilt, when it moves, to one direction or to another. For example:

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);

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

Since you didn't add any code I am not sure how you are moving your rocket, so I will post a generic code you will need to adapt depending on your own project:

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 游戏,您可能有兴趣查看本教程:

As an aside, you are doing a space 2D game you may be interested in checking this tutorial:

https://unity3d.com/learn/tutorials/s/space-射手教程

这篇关于Unity 3D - 没有旋转轴的旋转游戏对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆