OnCollisionEnter2D(Collision2D other) problem

by serz   Last Updated February 10, 2019 23:13 PM

I am making a "endless" runner game where he climbs up a "tower" of sorts and once he hits a wall on either side it turns him around and i got it working but for some reason right after he swaps direction he stops after about 0.5 seconds and won't start moving untill i jump..

my code:

ignore all the greyed out parts and its probably not well optimized because this is my first time with c# and ive been at it for a few weeks :P

the part im having a problem with is at the bottom and my jump and normal run code is near the top

{

public float moveSpeed;
private float moveSpeedStore;

public float jumpForce;

public float jumpTime;
private float jumpTimeCounter;

private bool stoppedJumping;
private bool canDoubleJump;

private Rigidbody2D myRigidBody;

public bool grounded;
public LayerMask whatIsGround;
public Transform groundCheck;
public float groundCheckRadius;

//private Collider2D myCollider;

private Animator myAnimator;

public GameManager theGameManager;

public AudioSource jumpSound;
public AudioSource deathSound;


// Start is called before the first frame update
void Start()
{
    myRigidBody = GetComponent<Rigidbody2D>();

    //myCollider = GetComponent<Collider2D>();

    myAnimator = GetComponent<Animator>();

    jumpTimeCounter = jumpTime;

    stoppedJumping = true;

    moveSpeedStore = moveSpeed;
}

// Update is called once per frame
void Update()
{

    //grounded = Physics2D.IsTouchingLayers(myCollider, whatIsGround);

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

    myRigidBody.velocity = new Vector2(moveSpeed, myRigidBody.velocity.y);

    if (Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0))
    {

        if (grounded)
        {
            myRigidBody.velocity = new Vector2(myRigidBody.velocity.x, jumpForce);
            stoppedJumping = false;
            jumpSound.Play();
        }

        if (!grounded && canDoubleJump)
        {
            myRigidBody.velocity = new Vector2(myRigidBody.velocity.x, jumpForce);

            stoppedJumping = false;
            canDoubleJump = false;
            jumpSound.Play();
        }
    }

    /*if ((Input.GetKey(KeyCode.Space) || Input.GetMouseButton(0)) && !stoppedJumping)
    {
        if (jumpTimeCounter > 0)
        {
            myRigidBody.velocity = new Vector2(myRigidBody.velocity.x, jumpForce);
            jumpTimeCounter -= Time.deltaTime;
        }
    }

    if (Input.GetKeyUp(KeyCode.Space) || Input.GetMouseButtonUp(0))
    {
        jumpTimeCounter = 0;
        stoppedJumping = true;
    }

    if (grounded)
    {
        jumpTimeCounter = jumpTime;
        canDoubleJump = false;
    }*/


    myAnimator.SetFloat("Speed", myRigidBody.velocity.x);
    myAnimator.SetBool("Grounded", grounded);
}

void OnCollisionEnter2D(Collision2D other)
{
    if (other.gameObject.tag == "killbox")
    {

        theGameManager.RestartGame();
        moveSpeed = moveSpeedStore;
        deathSound.Play();
    }

    if (other.gameObject.tag == "swap")
    {
        moveSpeed = -moveSpeed;
    }
}

}

Tags : unity


Related Questions


How to animate objects in Unity 3D

Updated July 12, 2015 13:05 PM