I have a basic prototype for v2 of my game and I have added a RigidBody2D to my character object. I’ve added a BoxCollider2D and I have BoxCollider2Ds on my platforms. I’m setting the velocity of the player in FixedUpdate and sometimes she gets stuck on a platform. Seems to be random. I have to jump to get over it. To combat this, I’ve added a CircleCollider2D to my players feet. A tactic I’ve seen used a fair bit. This prevents her from getting stuck but it causes her to bounce up off the “snagging” collider. It’s almost as if there is a slight gap randomly (there isn’t, the platforms are instantiated at runtime)
Here’s a clip of what’s happening. Note that I have simply attached the camera as a child of the player for now.
http://www.nfnproductions.com/Videos/CraneJumper2-bounce.mp4
Here is my script attached to my player:
namespace NfnProductions.CraneJumper.Unity.Assets.Scripts {
using UnityEngine;
public class Player : MonoBehaviour {
private Rigidbody2D _rigidbody;
public float MaxSpeed = 10f;
public float CurSpeed = 0f;
public float SpeedIncrementor = 0.1f;
public float JumpForce = 700f;
public bool IsGrounded = false;
public Transform GroundCheck;
public float GroundRadius = 0.2f;
public LayerMask WhatIsGround;
private void Start() {
_rigidbody = GetComponent<Rigidbody2D>();
}
private void FixedUpdate() {
IsGrounded = Physics2D.OverlapCircle(GroundCheck.position, GroundRadius, WhatIsGround);
Debug.Log("Grounded: " + IsGrounded.ToString());
if (Input.GetKey("x")) {
Debug.Log("Sprint Pressed");
this.CurSpeed += SpeedIncrementor;
if (CurSpeed > MaxSpeed) CurSpeed = MaxSpeed;
_rigidbody.velocity = new Vector2(this.CurSpeed, _rigidbody.velocity.y);
}
else {
this.CurSpeed = _rigidbody.velocity.x;
}
}
private void Update() {
if (this.IsGrounded && Input.GetKeyDown(KeyCode.Space)) {
_rigidbody.AddForce(new Vector2(0, JumpForce));
}
}
}
}
I’m using velocity but I have tried using AddForce. I have removed the BoxCollider2D from the player (so I only have a CircleCollider2D. I have played around with Gravity and GravityScale on the RigidBody2D (My overall Gravity is set to -30). I’ve checked there are no gaps in the colliders, I’ve reduced the number of platforms in case it’s a performance issue.
I’m all out of ideas. I’m sure it’s something simple, this is just a basic function so I must have done something wrong.
Thanks
UPDATE - 2016-04-03:
I’ve found that it is to do with the platform colliders. I have instantiated just one platform but with an X scale of 250 meaning I have 1 giant horizontal platform. The movement is smooth as I would expect.
I have tried using Edge colliders, still same.
This seems like real basic functionality and I’m in a new project so I must have something fundamentally wrong or others must surely be facing this problem! I can’t find anything on the latter so it must be something I have done!
Update 2 - 2016-04-03:
I have recreated it in a new Unity project (Unity 5.3.3.f1) and uploaded it here if anyone would like a look. When it’s running, hold “x” on your keyboard to add velocity to the player. She will bounce along sometimes (you may have to look carefully)
Steps I did.
- New Unity 2d Project
- Added Crane Part as a prefab, added box collider 2d
- Add player, gave rigidbody2d, set constraints z to fixed. Added circle collider 2d to her feet.
- Added the script to add velocity to the player