微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Android刷卡和addforce统一

我正在研究2D无限跑者.我有以下代码从屏幕滑动输入跳转,滑动和快速运行.我从编辑器提供jumpHeight,值为500,帧率为30.代码工作正常,但有时候玩家跳得太高而无法刷卡.如果输入来自键盘,则类似代码按预期工作.为什么会发生这种情况超出了我对统一的理解.任何帮助是极大的赞赏.

using UnityEngine;
public class PlayerControl : MonoBehavIoUr
{

    public float ForwardSpeed = 3.7f; //moves player in forward direction
    public float speedOffset = 0.0f; //offset speed of player
    public float JumpHeight = 250; //moves player in verticle direction
    bool grounded = false; //checks if player is grounded or not
    public Transform groundCheck;
    float groundCheckRadius = 0.3f; //radius of groundcheck circle to check grounded bool
    public LayerMask groundLayer;
    Vector2 fingerStart;
    Vector2 fingerEnd;

    void Update()
    {
        foreach (Touch touch in Input.touches)
        {
            if (touch.phase == TouchPhase.Began)
            {
                fingerStart = touch.position;
                fingerEnd = touch.position;
            }

            if (touch.phase == TouchPhase.Moved)
            {
                fingerEnd = touch.position;
                if (Mathf.Abs(fingerEnd.y - fingerStart.y) > 50)//Vertical swipe
                {
                    if (fingerEnd.y - fingerStart.y > 50)//up swipe
                    {
                        Jump();
                    }
                    else if (fingerEnd.y - fingerStart.y < -50)//Down swipe
                    {
                        //Slide();
                    }
                    fingerStart = touch.position;
                }
            }
            if (touch.phase == TouchPhase.Stationary)
            {
                RunFast();
            }
            if (touch.phase == TouchPhase.Ended)
            {
                fingerEnd = touch.position;
                if (Mathf.Abs(fingerEnd.y - fingerStart.y) > 50)//Vertical swipe
                {
                    if (fingerEnd.y - fingerStart.y > 50)//up swipe
                    {
                        Jump();
                    }
                    else if (fingerEnd.y - fingerStart.y < -50)//Down swipe
                    {
                        //Slide();
                    }
                }
            }
        }



        if (Input.GetButton("Fire1"))
        {
            speedOffset = 2.5f;
        }
        else
        {
            speedOffset = 0.0f;
        }


        if (grounded && Input.GetKeyDown(KeyCode.UpArrow))
        {
            grounded = false;
            GetComponent<Rigidbody2D>().AddForce(new Vector2(0, JumpHeight));
        }

        //check if circle overlaps with ground layer
        grounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, groundLayer);
        //Debug.Log(grounded);


    }



    void FixedUpdate()
    {
        //set players forward veLocityto forward speed variable
        Vector2 PlayerForwardveLocity = GetComponent<Rigidbody2D>().veLocity;
        // Vector2 PlayerJumpHeight = GetComponent<Rigidbody2D>().AddForce()
        PlayerForwardveLocity.x = ForwardSpeed + speedOffset;
        GetComponent<Rigidbody2D>().veLocity = PlayerForwardveLocity;
    }

    void Jump()
    {
        if (grounded)
        {
            GetComponent<Rigidbody2D>().AddForce(new Vector2(0, JumpHeight));
            speedOffset = 0.0f;
        }

    }

    void RunFast()
    {
        if (Input.GetButton("Fire1"))
        {
            speedOffset = 2.5f;
        }
        else
        {
            speedOffset = 0.0f;
        }
    }
}

解决方法:

您的代码有2个问题.

你的第一个问题在于这行代码

 grounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, groundLayer);

这行代码失败了.接地总是如此.因此,当玩家没有接地时,跳跃被称为太多次.

用以下代码替换此行代码

 Collider2D playerCollider = gameObject.GetComponent<Collider2D>();
 grounded = Physics2D.OverlapCircle(playerCollider.transform.position, 1, groundLayer); 

要么

Collider2D playerCollider = gameObject.GetComponent<Collider2D>();
grounded = playerCollider.IsTouchingLayers(groundLayer.value);

你的代码的另一个问题是错误的报告.有时,对撞机重叠返回true,即使它是假的.我尝试将该部分代码移动到LateUpdate函数但是没有修复它.

您可以通过实现计时器来解决它.当玩家跳跃时,计时器重置为0并开始计数到0.5.当计时器未达到计数值时,请勿跳转. .5到1是一个完美的价值.使用Time.deltaTime增加计时器.下面是您的整个代码和计时器和修复程序.

public class PlayerControl : MonoBehavIoUr
{

    public float ForwardSpeed = 3.7f; //moves player in forward direction
    public float speedOffset = 0.0f; //offset speed of player
    public float JumpHeight = 250; //moves player in verticle direction
    bool grounded = false; //checks if player is grounded or not
    public Transform groundCheck;
    float groundCheckRadius = 0.3f; //radius of groundcheck circle to check grounded bool
    public LayerMask groundLayer;
    Vector2 fingerStart;
    Vector2 fingerEnd;

    public float resetTimer = 0.5f; //.5 second
    float timerCounter = 0;
    Collider2D playerCollider = null;

    Rigidbody2D playerRigidBody;


    void Start()
    {
        playerRigidBody = GetComponent<Rigidbody2D>();
        playerCollider = gameObject.GetComponent<Collider2D>();
    }

    void Update()
    {

        foreach (Touch touch in Input.touches)
        {
            if (touch.phase == TouchPhase.Began)
            {
                fingerStart = touch.position;
                fingerEnd = touch.position;
            }

            if (touch.phase == TouchPhase.Moved)
            {
                fingerEnd = touch.position;
                if (Mathf.Abs(fingerEnd.y - fingerStart.y) > 50)//Vertical swipe
                {
                    if (fingerEnd.y - fingerStart.y > 50)//up swipe
                    {
                        Jump();
                    }
                    else if (fingerEnd.y - fingerStart.y < -50)//Down swipe
                    {
                        //Slide();
                    }
                    fingerStart = touch.position;
                }
            }
            if (touch.phase == TouchPhase.Stationary)
            {
                RunFast();
            }
            if (touch.phase == TouchPhase.Ended)
            {
                fingerEnd = touch.position;
                if (Mathf.Abs(fingerEnd.y - fingerStart.y) > 50)//Vertical swipe
                {
                    if (fingerEnd.y - fingerStart.y > 50)//up swipe
                    {
                        Jump();
                    }
                    else if (fingerEnd.y - fingerStart.y < -50)//Down swipe
                    {
                        //Slide();
                    }
                }
            }
        }



        if (Input.GetButton("Fire1"))
        {
            speedOffset = 2.5f;
        }
        else
        {
            speedOffset = 0.0f;
        }


        if (grounded && Input.GetKeyDown(KeyCode.UpArrow))
        {
            grounded = false;
            playerRigidBody.AddForce(new Vector2(0, JumpHeight));
        }

        //check if circle overlaps with ground layer
        grounded = Physics2D.OverlapCircle(playerCollider.transform.position, 1, groundLayer);
        //OR Use grounded = playerCollider.IsTouchingLayers(groundLayer.value);

        //Increment Timer if it is still less than resetTimer
        if (timerCounter < resetTimer)
        {
            timerCounter += Time.deltaTime;
        }
    }


    void FixedUpdate()
    {
        //set players forward veLocityto forward speed variable
        Vector2 PlayerForwardveLocity = playerRigidBody.veLocity;
        // Vector2 PlayerJumpHeight = playerRigidBody.AddForce()
        PlayerForwardveLocity.x = ForwardSpeed + speedOffset;
        playerRigidBody.veLocity = PlayerForwardveLocity;
    }

    void Jump()
    {
        if (grounded)
        {
            //Exit if timer has not reached the required value to jump again
            if (timerCounter < resetTimer)
            {
                Debug.Log("Failed To Jump because timer has not yet reached");
                return; //Exit
            }

            timerCounter = 0; //Reset Timer

            playerRigidBody.AddForce(new Vector2(0, JumpHeight));
            speedOffset = 0.0f;
            Debug.Log("Jumped");
        }
        else
        {
            Debug.Log("Not on the Ground");
        }

    }

    void RunFast()
    {
        if (Input.GetButton("Fire1"))
        {
            speedOffset = 2.5f;
        }
        else
        {
            speedOffset = 0.0f;
        }
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐