0
\$\begingroup\$

My project is an infinite runner that goes right in a straight line. Sometimes when it just runs without jumping the Player stops. Sometimes the Player doesn't stop. When you jump the Player usually stops a lot earlier. I've been trying to fix this, but I can't find the solution.

How I'm doing this is by making the character move to the right all the time with 1 point in the front for ground generating and 1 point in the back for deleting ground. The camera is following the player and the points are going forward at the same pace as the player. The ground is even, but the player always stops. Please help.

Code for generating the ground:

using UnityEngine;
using System.Collections;

public class PlatformGenerator : MonoBehaviour {

public GameObject thePlatform;
public Transform generationPoint;
public float distanceBetween;

private float platformWidth;

// Use this for initialization
void Start () {
    platformWidth = thePlatform.GetComponent<BoxCollider2D>().size.x;
}

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

    if (transform.position.x < generationPoint.position.x) 
    {
        transform.position = new Vector3 (transform.position.x + platformWidth + distanceBetween, transform.position.y, transform.position.z);

        Instantiate (thePlatform, transform.position, transform.rotation);
    }

}

}

Code for controlling the Player:

using UnityEngine;
using System.Collections;

public class PlayerControl : MonoBehaviour {

public float moveSpeed;
public float jumpForce;

private Rigidbody2D myRigidbody;

public bool grounded;
public LayerMask whatIsGround;

private Collider2D myCollider;

private Animator myAnimator;

// Use this for initialization
void Start () {
    myRigidbody = GetComponent<Rigidbody2D>();
    myRigidbody.velocity = new Vector2(moveSpeed, 0);

    myCollider = GetComponent<Collider2D>();

    myAnimator = GetComponent<Animator> ();
}

// Update is called once per frame
//void Update () {
//  myRigidbody.velocity = new Vector2(moveSpeed, myRigidbody.velocity.y);
//
//  if(Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0));
//  {
//      myRigidbody.velocity=new Vector2(myRigidbody.velocity.x, jumpForce);
//  }
void Update () { 

    grounded = Physics2D.IsTouchingLayers(myCollider, whatIsGround);

    if (Input.GetKeyDown(KeyCode.Space))
    { 
        if(grounded)
        {
            Jump();
        }   

    }

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

}

void Jump () {
    myRigidbody.AddForce(new Vector2(0, jumpForce));
    }

}

Unity screen

\$\endgroup\$
5
  • 1
    \$\begingroup\$ If the player is walking on "several grounds" - (more than 1 platform, connected) it might get stuck on some corners of the platform. (even though they might not be visible at all when zooming in) this can be fixed using a circle collider if I remember correctly. (on the player) \$\endgroup\$
    – BiiX
    Commented Feb 9, 2016 at 8:35
  • 1
    \$\begingroup\$ Tell me if it works! :) \$\endgroup\$
    – BiiX
    Commented Feb 9, 2016 at 8:47
  • \$\begingroup\$ @BiiX You wonderful son of a bitch! :D It works better, but sometimes it gets stuck a little bit and slows down. :/ I think that I'll just add it so that the speed gets a little bit faster all the time, so It doesn't matter if the player slows down a little. There must be a better solution though. \$\endgroup\$ Commented Feb 9, 2016 at 9:00
  • 1
    \$\begingroup\$ I'll ad a answer with a possible 2nd fix for that. :) \$\endgroup\$
    – BiiX
    Commented Feb 9, 2016 at 9:00
  • \$\begingroup\$ The 2nd fix on my answer might fix that :) \$\endgroup\$
    – BiiX
    Commented Feb 9, 2016 at 9:40

3 Answers 3

1
\$\begingroup\$

If you have several platforms connected in Unity your character might sometimes, (not always) slow down/stop.

There are 2 fixes that might work for this :


1. Circle Collider

    If you have a box collider on your moving object/character, the corners of the collider might collide with the corners on the connected platforms, causing the object to slow down while it's trying to go over it, or just stop completely. To fix this, simply replace the players box collider with a circle collider instead.

2. Physics Material

    If your still having the same problem as before, you can try to apply a physics material to the player and play around with the values until it gets "un-stuck" on the platforms.
\$\endgroup\$
4
  • \$\begingroup\$ Thanks. I have Slippery physics material already. I can't upvote, but I marked you answer as the accepted answer. Unless someone makes a better answer. >:) \$\endgroup\$ Commented Feb 9, 2016 at 9:44
  • \$\begingroup\$ @SleepySleeper Not sure how a slippery material is like, but if you haven't already tried : set friction on the physics material to 0. :D \$\endgroup\$
    – BiiX
    Commented Feb 9, 2016 at 10:01
  • \$\begingroup\$ It's the same thing. I had it before I even asked this question. \$\endgroup\$ Commented Feb 9, 2016 at 10:03
  • \$\begingroup\$ Changed BoxCollider2D to PolygonCollider2D. Seems to work good too. \$\endgroup\$
    – Luke359
    Commented Dec 20, 2018 at 8:04
0
\$\begingroup\$

Try decreasing the Default Contact Offset in Edit/Project Settings/Physics 2D

Changing it from 0.01 to 0.0001 worked for me

Source

\$\endgroup\$
0
\$\begingroup\$

You can set the Edge Radius of the BoxCollider to something like 0.1 Then the Edges are rounded and it does not get stuck on a tiled plan Ground. Or use a CapsuleCollider.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .