简体   繁体   English

如何在 Unity 2D 中旋转 object,然后在该方向旋转 go?

[英]How do I rotate an object in Unity 2D and then go in that direction?

I am new to unity and C# but am now making a small game where you are a jellyfish in the sea.我是 unity 和 C# 的新手,但现在正在制作一个小游戏,你是海中的水母。 I want to make it that you mainly control the player with the arrow keys to face in different directions.我想让你主要用箭头键控制玩家面向不同的方向。 The code below is what I have now, I thought it would work, but it doesn't.下面的代码是我现在拥有的,我认为它会起作用,但它没有。 Do I need to change something completely or is it just a small error by me?我需要彻底改变某些东西还是我的一个小错误? It is a 2D game.这是一个2D游戏。

    float  tpx = transform.position.x;
    float  tpy = transform.position.y;


    if (Input.GetKey("up")){
        if (face_up){
            transform.position = new Vector2 (tpx, tpy + speed/1000);
            animation.SetBool("swim", true);
        }

        if (face_right){
            transform.position = new Vector2 (tpx + speed/1000, tpy);
            animation.SetBool("swim", true);
        }

        if (face_down){
            transform.position = new Vector2 (tpx, tpy + speed/1000);
            animation.SetBool("swim", true);
        }

        if (face_left){
            transform.position = new Vector2 (tpx - speed/1000, tpy);
            animation.SetBool("swim", true);
        }
    }
    else{
        animation.SetBool("swim", false); gameObject.transform.position = new Vector2 (transform.position.x, transform.position.y - speed/10000);
    }


    if (Input.GetKey("left")){
        if (face_up){
            transform.eulerAngles = Vector3.forward * 90;
            face_up = false;
            face_right = false;
            face_down = false;
            face_left = true;
        }

        if (face_right){
            transform.eulerAngles = Vector3.forward * 0;
            face_up = true;
            face_right = false;
            face_down = false;
            face_left = false;
        }
    }

    if (Input.GetKey("right")){
        if (face_up){
            transform.eulerAngles = Vector3.forward * -90;
            face_up = false;
            face_right = true;
            face_down = false;
            face_left = false;
        }

        if (face_left){
            transform.eulerAngles = Vector3.forward * 0;
            face_up = true;
            face_right = false;
            face_down = false;
            face_left = false;
        }
    }

There are indeed many flaws in your code.你的代码确实有很多缺陷。

First you should not move your sprite by directly changing its position. There are different tools in Unity to do so, check out the Translate function. It will greatly help you since you don't need to know the current rotation of your sprite (you seem to misunderstand what is Vector3.forward).首先,你不应该通过直接改变它的 position 来移动你的精灵。Unity 中有不同的工具可以这样做,查看Translate function。它会对你有很大帮助,因为你不需要知道你的精灵的当前旋转(你似乎误解了什么是 Vector3.forward)。 Also check out how Time.deltaTime works in this function.另请查看 Time.deltaTime 在此 function 中的工作原理。

Second, when rotating your sprite, you should do it relatively to its current rotation, ie add or substract 90 degrees.其次,当旋转你的精灵时,你应该相对于它当前的旋转角度来做,即增加或减少 90 度。 Look at Quaternion.Euler看看Quaternion.Euler

Finally you don't need that much booleans.最后你不需要那么多布尔值。 When programming you should try to avoid repetition, so when you have lots of the same code over and over, that's a good sign that you can write it better.在编程时,您应该尽量避免重复,因此当您一遍又一遍地编写大量相同的代码时,这是一个好兆头,表明您可以将代码写得更好。

A solution could be something along these line.解决方案可能是沿着这些路线的。 Keep in mind though that I didn't test it so the Vector3.up and the Vectors (0, 0, 90f) in Quaternion.Euler might be wrong depending on how you oriented your gameplay:请记住,虽然我没有测试它,但 Quaternion.Euler 中的 Vector3.up 和 Vectors (0, 0, 90f) 可能是错误的,具体取决于您如何定位游戏玩法:

    if (Input.GetKeyDown(KeyCode.UpArrow))
    {
        transform.Translate(Vector3.up * Time.deltaTime);
    }
    if (Input.GetKeyDown(KeyCode.RightArrow))
    {
        transform.rotation *= Quaternion.Euler(0, 0, 90f);
    }
    if (Input.GetKeyDown(KeyCode.LeftArrow))
    {
        transform.rotation *= Quaternion.Euler(0, 0, -90f);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM