简体   繁体   English

如何使玩家在Unity中上山

[英]How to Make Player Walk Up Hills in Unity

Hey guys so im making a 2D Platformer and ran into a difficult situation. 大家好,我即时制作了2D平台游戏,结果遇到了困难。 Up until now I have had rectangular landscapes for the player to jump and walk on. 直到现在,我还拥有矩形景观,供玩家跳跃和行走。 Now I want to start adding some hills. 现在,我要开始添加一些山丘。 Picture a triangle, the left side is representing a hill at which the player can walk up. 想象一个三角形,左侧代表一个小山,玩家可以在该小山上行走。 Ive made the landscape as well as copied over the Box Collider 2D from my other paths. Ive制作了风景并从其他路径复制到了Box Collider 2D上。 I figured out a way to rotate the collider so that it goes up at a 45 degree angle just like the slope of the triangle. 我想出了一种方法来旋转对撞机,使其以45度角上升,就像三角形的斜率一样。 For some reason though when I walk into the triangle the player doesnt start walking up it. 但是由于某种原因,当我进入三角形时,播放器不会开始向上移动。 I have to jump onto the hill and he lands on what looks like an invisible rectangle! 我必须跳上山坡,然后他降落在一个看不见的矩形上! I make the collider .25 in thickness so I know that I have the whole collider where I want it. 我将对撞机的厚度设定为.25,所以我知道整个对撞机都在需要的地方。 Any idea why? 知道为什么吗? I dont really have any code for this as everything Im doing has pretty much been in scene view. 我真的没有任何代码,因为我所做的一切几乎都在场景视图中。 Thanks everyone! 感谢大家!

Here is the Player in Play Mode, stands on top of hill by some force: 这是处于“播放”模式的玩家,用某种力量站在山顶上:

这是处于“播放”模式的玩家,用力站立在山顶上

Here is what the collider looks like in scene view. 这是对撞机在场景视图中的外观。 Ive moved the collider all over and it still does the same thing: Ive将对撞机全部移开,它仍然做同样的事情:

这是对撞机在场景视图中的外观。我已经将对撞机全部移动了,它仍然做同样的事情

Here is my movement code: 这是我的移动代码:

public class Player : MonoBehaviour {

    public float maxSpeed = 3;
    public float speed = 50f;
    public float jumpPower = 150f;

    public bool grounded;

    private Rigidbody2D rb2d;
    private Animator anim;

    void Start () {

        rb2d = gameObject.GetComponent<Rigidbody2D>();
        anim = gameObject.GetComponent<Animator>();
    }


    void Update () {

        anim.SetBool("Grounded",grounded);
        anim.SetFloat("Speed", Mathf.Abs(rb2d.velocity.x));

        if (Input.GetAxis("Horizontal") < -0.1)
        {
            transform.localScale = new Vector3(-1, 1, 1);

        }
        if (Input.GetAxis("Horizontal") > 0.1)
        {
            transform.localScale = new Vector3(1, 1, 1);

        }

        if (Input.GetButtonDown("Jump") && grounded)
        {
            rb2d.AddForce(Vector2.up * jumpPower);

        }

    }

    void FixedUpdate() {

        Vector3 easeVelocity = rb2d.velocity;
        easeVelocity.y = rb2d.velocity.y;
        easeVelocity.z = 0.0f;
        easeVelocity.x *= 0.75f; 

        float h = Input.GetAxis("Horizontal");

        //Fake friction / Easing the x speed of our player
        if (grounded)
        {

            rb2d.velocity = easeVelocity;


        }



        //Moving the Player
        rb2d.AddForce((Vector2.right * speed) * h);

        //Limiting the Speed of the Player
        if (rb2d.velocity.x > maxSpeed)
        {
            rb2d.velocity = new Vector2(maxSpeed, rb2d.velocity.y);

        }

        if (rb2d.velocity.x < -maxSpeed)
        {
            rb2d.velocity = new Vector2(-maxSpeed, rb2d.velocity.y);

        }
    }
}

The Ground Material is applied to this landscape just like the other rectangular landscape. 就像其他矩形景观一样,将地面材料应用于该景观。 Sorry Im new but again, thanks everyone! 对不起,我是新人,但再次感谢大家!

I believe you should take a look at the 2D Plaftormer Controller tutorial series by Sebastian Lague . 我相信您应该看一下Sebastian Lague2D Plaftormer Controller教程系列 The series is focused on creating a robust 2D platformer controller, and it is very well made. 该系列专注于创建强大的2D平台游戏控制器,并且制作精良。 I have watched the videos and built a working solution based on them. 我已经观看了视频,并基于它们构建了一个可行的解决方案。

The fourth episode of the series is focused on climbing slopes, and the next episode is focused on descending them. 该系列的第四集着重于攀登斜坡,下一集着重于下降斜坡。 But I still suggest that you should start from the beginning and follow the series patiently. 但是我仍然建议您从头开始,并耐心地遵循该系列。 Lague always offers very detailed explanations to why he implements thing a certain way. Lague总是对他为什么以某种方式实现事物提供非常详细的解释。

I'm new to Unity as well but a thought I had was...is it possible your collider dimensions are resetting after the first frame? 我也是Unity的新手,但是我曾经想过……是否可能在第一帧之后重置对撞机尺寸? Maybe you can manually set the collider to stay at a certain angle to test this. 也许您可以手动设置对撞机使其保持一定角度以进行测试。 Just a thought! 只是一个想法! I'd be interested to know what you find out. 我很想知道您的发现。

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

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