簡體   English   中英

Unity 2d Character 使用 UI 按鈕移動和跳轉到特定位置

[英]Unity 2d Character moves and jumps to specific positions with UI buttons

在一個簡單的 2d 項目中,我的播放器可以在 x 上向右和向左移動,也可以用另外兩個按鈕在高度上向右和向左跳躍。 問題是,玩家不應該自由移動。 通過按下其中一個按鈕,玩家應該只 go 到下一個特定點,以便玩家總是停在 x 上的六個不同位置(而在 y 上,他是自由的,並且與他當前站立的平台一樣高)。 為了能夠真實地跳躍,玩家必須有重力和碰撞器才能降落在平台上(並水平移動單個平台)。 感謝@TEEBQNE 在評論中鏈接的教程,我終於可以使用 Unitys Rigidbody2D 和以下腳本實現這一點。 問題是重力現在表現得很奇怪。 玩家只會非常緩慢地向下移動,並在此過程中將游戲對象推入其下方,使其穿過其他對象。 玩家有一個重力為 2 的 Dynamic Rigidbody2D 和一個 2d 的膠囊對撞機。 這是腳本問題還是玩家檢查器中的組件有問題?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class movePlayer : MonoBehaviour
{

    public GameObject posChecker1;
    public GameObject posChecker2;
    public GameObject posChecker3;
    public GameObject posChecker4;
    public GameObject posChecker5;
    public GameObject posChecker6;

    public bool go; //Player is allowed to move
    public bool grounded; //Player is allowed to jump
    public string moveDirection;
    public float horizVel = 0; //Movement along x
    public float verticVel = 0; //Jump

    public int laneNum = 3; //Player starts on lane 3!!
    public bool rightButtonMove = false;//
    public bool leftButtonMove = false;//
    public bool rightButtonJump = false;//
    public bool leftButtonJump = false;//



    //Animation
    private SpriteRenderer spriteRenderer;
    private Animator animator;

    private void Awake()
    {
        spriteRenderer = GetComponent<SpriteRenderer>();
        animator = GetComponent<Animator>();
    }



    // Start is called before the first frame update
    void Start()
    {
        posChecker1.SetActive(true);//GameObject.Find("PositionChecker1").SetActive(true);
        posChecker2.SetActive(true);
        posChecker3.SetActive(true); //Player start on lane 3!!
        posChecker4.SetActive(true);
        posChecker5.SetActive(true);
        posChecker6.SetActive(true);

        laneNum = 3;
        go = true;
    }

    // Update is called once per frame
    void Update()
    {
        //Raycast
        int playerMask = LayerMask.GetMask("PositionChecker");// !!!

        Debug.DrawRay(transform.position, transform.TransformDirection(Vector2.up) * 50f, Color.green);
        RaycastHit2D hitCheck = Physics2D.Raycast(transform.position, transform.TransformDirection(Vector2.up), 50f, playerMask);

        //Only the checker objects in the rows next to the player are active
        if (laneNum == 1)
        {
            posChecker1.SetActive(false);
            posChecker2.SetActive(true);
        }
        else if (laneNum == 2)
        {
            posChecker1.SetActive(true);
            posChecker2.SetActive(false);
            posChecker3.SetActive(true);
        }
        else if (laneNum == 3)
        {
            posChecker2.SetActive(true);
            posChecker3.SetActive(false);
            posChecker4.SetActive(true);
        }
        else if (laneNum == 4)
        {
            posChecker3.SetActive(true);
            posChecker4.SetActive(false);
            posChecker5.SetActive(true);
        }
        else if (laneNum == 5)
        {
            posChecker4.SetActive(true);
            posChecker5.SetActive(false);
            posChecker6.SetActive(true);
        }
        else if (laneNum == 6)
        {
            posChecker5.SetActive(true);
            posChecker6.SetActive(false);
        }

        //Movement
        GetComponent<Rigidbody2D>().velocity = new Vector3(horizVel, verticVel, 0);


        //Raycast
        if (hitCheck)
        {
            
            if (moveDirection == "l" && horizVel != 0)
            {
                laneNum -= 1;
            }
            if (moveDirection == "r" && horizVel != 0)
            {
                laneNum += 1;
            }
            go = true;
            horizVel = 0;
            verticVel = 0;
            grounded = true;
        }
        if (horizVel == 0)
            moveDirection = "";


        //Animation
        bool flipSprite = (spriteRenderer.flipX ? (horizVel > 0.01f) : (horizVel < 0.01f));
        if (flipSprite)
        {
            spriteRenderer.flipX = !spriteRenderer.flipX;
        }

        animator.SetBool("grounded", grounded); // -->Jump
        animator.SetFloat("velocityX", Mathf.Abs(horizVel));

    }


    //Button Input
    public void RightButton() //
    {
        if (laneNum < 6 && go)
        {
            moveDirection = "r";
            go = false;
            horizVel = 4;
        }
    }
    public void LeftButton()//
    {
        if (laneNum > 1 && go)
        {
            moveDirection = "l";
            go = false;
            horizVel = -4;
        }
    }
    public void RightJump()//
    {
        if (laneNum < 6 && grounded && go)
        {
            moveDirection = "r";
            horizVel = 4;
            verticVel = 7;
            go = false;
            grounded = false;
        }
    }
    public void LeftJump()//
    {
        if (laneNum > 1 && grounded && go)
        {
            moveDirection = "l";
            horizVel = -4;
            verticVel = 7;
            go = false;
            grounded = false;
        }
    }
}

很高興我能夠以某種方式提供幫助,並且您解決了您的問題!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class movePlayer : MonoBehaviour
{
    //Player Position x
    public GameObject posChecker1;
    public GameObject posChecker2;
    public GameObject posChecker3;
    public GameObject posChecker4;
    public GameObject posChecker5;
    public GameObject posChecker6;
    public int laneNum = 3; //Player starts on lane 3!!

    //Player Position y
    public float yPos1;
    public float yPos2;
    public Transform player;

    //Movement Variables
    public bool go; //Player is allowed to move
    public bool grounded; //Player is allowed to jump
    public string moveDirection;
    public float horizVel = 0; //Movement along x
    public float verticVel = 0; //Jump

    //Animation
    private SpriteRenderer spriteRenderer;
    private Animator animator;

    private void Awake()
    {
        spriteRenderer = GetComponent<SpriteRenderer>();
        animator = GetComponent<Animator>();
    }



    // Start is called before the first frame update
    void Start()
    {
        posChecker1.SetActive(true);//GameObject.Find("PositionChecker1").SetActive(true);
        posChecker2.SetActive(true);
        posChecker3.SetActive(true); //Player start on lane 3!!
        posChecker4.SetActive(true);
        posChecker5.SetActive(true);
        posChecker6.SetActive(true);

        laneNum = 3;
        go = true;
    }

    // Update is called once per frame
    void Update()
    {
        //Raycast
        int playerMask = LayerMask.GetMask("PositionChecker");// !!!

        Debug.DrawRay(transform.position, transform.TransformDirection(Vector2.up) * 50f, Color.green);
        RaycastHit2D hitCheck = Physics2D.Raycast(transform.position, transform.TransformDirection(Vector2.up), 50f, playerMask);

        //Only the checker objects in the rows next to the player are active
        if (laneNum == 1)
        {
            posChecker1.SetActive(false);
            posChecker2.SetActive(true);
        }
        else if (laneNum == 2)
        {
            posChecker1.SetActive(true);
            posChecker2.SetActive(false);
            posChecker3.SetActive(true);
        }
        else if (laneNum == 3)
        {
            posChecker2.SetActive(true);
            posChecker3.SetActive(false);
            posChecker4.SetActive(true);
        }
        else if (laneNum == 4)
        {
            posChecker3.SetActive(true);
            posChecker4.SetActive(false);
            posChecker5.SetActive(true);
        }
        else if (laneNum == 5)
        {
            posChecker4.SetActive(true);
            posChecker5.SetActive(false);
            posChecker6.SetActive(true);
        }
        else if (laneNum == 6)
        {
            posChecker5.SetActive(true);
            posChecker6.SetActive(false);
        }

        //Movement
        if (grounded)
        {
            verticVel = GetComponent<Rigidbody2D>().velocity.y;
        }
        GetComponent<Rigidbody2D>().velocity = new Vector3(horizVel, /*GetComponent<Rigidbody2D>().velocity.y*/verticVel, 0);
        //Jump
        yPos2 = player.transform.position.y;
        if ((yPos2 - 2) >= yPos1 && !grounded)
        {
            if (moveDirection == "l")
                horizVel = -4;
            if (moveDirection == "r")
                horizVel = 4;
            verticVel = verticVel * 0.95f;
        }

        //Raycast
        if (hitCheck)
        {
            if (moveDirection == "l" && horizVel != 0)
            {
                laneNum -= 1;
            }
            if (moveDirection == "r" && horizVel != 0)
            {
                laneNum += 1;
            }
            go = true;
            horizVel = 0;
            verticVel = 0;
            grounded = true;
        }
        if (horizVel == 0 && grounded)
            moveDirection = "";


        //Animation
        bool flipSprite = (spriteRenderer.flipX ? (horizVel > 0.01f) : (horizVel < 0.01f));
        if (flipSprite)
        {
            spriteRenderer.flipX = !spriteRenderer.flipX;
        }

        animator.SetBool("grounded", grounded); // -->Jump
        animator.SetFloat("velocityX", Mathf.Abs(horizVel));

    }


    //Button Input
    public void RightButton() //
    {
        if (laneNum < 6 && go)
        {
            moveDirection = "r";
            go = false;
            horizVel = 4;
        }
    }
    public void LeftButton()//
    {
        if (laneNum > 1 && go)
        {
            moveDirection = "l";
            go = false;
            horizVel = -4;
        }
    }
    public void RightJump()//
    {
        if (laneNum < 6 && grounded && go)
        {
            moveDirection = "r";
            verticVel = 5;
            go = false;
            grounded = false;
            //
            yPos1 = player.transform.position.y;
            Debug.Log(yPos1);
            //
        }
    }
    public void LeftJump()//
    {
        if (laneNum > 1 && grounded && go)
        {
            moveDirection = "l";
            verticVel = 5;
            go = false;
            grounded = false;
            //
            yPos1 = player.transform.position.y;
            Debug.Log(yPos1);
            //
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM