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

相关文章

前言 本文记录unity3D开发环境的搭建 unity安装 unity有中文...
前言 有时候我们希望公告牌跟随镜头旋转永远平行面向屏幕,同...
前言 经过一段时间的学习与实际开发,unity3D也勉强算是强行...
前言 在unity中我们常用的获取鼠标点击的方法有: 1、在3D场...
前言 在之前的例子中,我们都没有用到unity的精髓,例如地形...
这篇文章将为大家详细讲解有关Unity3D中如何通过Animator动画...