Destroying Android and adding power

I am working on a 2D endless runner. I have the code below to enter screen input to jump, slide and run fast. I provide jumpHeight from the editor, and the value is 500 with a frame rate of 30. The code works fine, but sometimes the player jumps too high for a swipe. Similar code works as expected if input from the keyboard. Why is this happening outside my understanding of unity. Any help is appreciated.

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;
        }
    }
}
+4
source share
1 answer

You have 2 problems with your code.

Your first problem is this line of code:

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

. grounded true. - , .

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

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

- false. true, false. LateUpdate, .

, . 0 0,5, . , , . .5 to 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;
        }
    }
}
+1

All Articles