- Home /
2D Character suddenly stops moving for no given reason
Currently, I am attempting to make a 2D 8-bit space RPG thing where the player moves around on procedurally generated planets following some sort of quest. I have the terrain generation all set up, but I am stuck on making my character move. I have not yet implemented jumping (I want to get this out of the way first), but when I try to move right, everything will work as expected, until suddenly, my character completely stops.
Upon writing Input.GetAxisRaw("Horizontal") and transform.position.x to the console, I found out that nothing changed to the input (still was at 1 or -1), yet the position didn't even move the slightest. I do have animations set up, but disabling the Animator component does not help. All the tiles for the planets are made from prefabs, and each has a square collider, which I have checked, and made sure that they are not acting out of the ordinary. When the character halts, he is not colliding with anything. The rigidbody2d has continuous collision detection, a gravity scale of 15, and has constrained rotation along the z axis.
I hope that this problem can be rectified soon, as my small indie team and I are working towards a deadline. Here is the only script controlling or manipulating the character in any way (the character controller):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour {
private Animator playerAnimator;
public float moveSpeed; // set to 5 in editor
// Use this for initialization
void Start () {
playerAnimator = GetComponent<Animator> ();
}
// Update is called once per frame
void Update () {
//Handling animations
if (Input.GetAxisRaw ("Horizontal") == 1.0f) { //Moving right
setWalkingDirection (4);
} else if (Input.GetAxisRaw ("Horizontal") == -1.0f) { // Moving left
setWalkingDirection (3);
} else { // Not moving at all
setWalkingDirection (0);
}
//Player input
Vector2 move = new Vector2(Input.GetAxisRaw("Horizontal"), 0.0f) * moveSpeed;
GetComponent<Rigidbody2D> ().velocity = move;
}
public void setWalkingDirection(int direction) {
playerAnimator.SetInteger ("WalkingDirection", direction);
}
}
Sudden stopping of movement rather suggests a collision. Are you sure you're not colliding with anything? Have you tried printing output of OnCollisionStay2D?
When I step from tile to tile, and collide with a building, OnCollisionStay2D gets called. This does not stop when I mysteriously get blocked, so I am not sure if this is of any use or not.
Answer by MelvMay · Dec 17, 2016 at 10:52 AM
http://www.iforce2d.net/b2dtut/ghost-vertices
The fact that you're also using a huge gravity is probably making this worse as the collider drifts up/down slight (only fractionally) and easily gets caught on the 'side' edge of adjacent colliders. Dump out the collision normal and you'll probably find that you get a sideways normal which is what stops the character.
Multiple colliders do not form a continuous surface no matter how you align them. A single EdgeCollider2D provides multiple edges that form a continuous surface which solves this problem. Additionally, coming in 5.6 (available in beta now) as well as in the current 2D experimental previews is the CompositeCollider2D that allows you to merge multiple colliders into a single continuous surface.
Wow! Thank you so much! Changing from box to edge colliders fixed the problem completely! I am so happy as now I can finally get working on my game again :). Unity 5.6's CompositeCollider2D sounds interesting, so I look forward to it's eventual release. You've been such a help :)