• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by TheUltimateKerbonaut · Dec 17, 2016 at 08:39 AM · collisionmovementrigidbody2dcharacter controllerstopping

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);
     }
 }
 

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image tanoshimi · Dec 17, 2016 at 08:41 AM 0
Share

Sudden stopping of movement rather suggests a collision. Are you sure you're not colliding with anything? Have you tried printing output of OnCollisionStay2D?

avatar image TheUltimateKerbonaut tanoshimi · Dec 17, 2016 at 08:51 AM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

https://www.youtube.com/watch?v=H6RdrEb77cc

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TheUltimateKerbonaut · Dec 17, 2016 at 12:24 PM 0
Share

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 :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Player movement ignores some collision 0 Answers

How Do I Make a Rigidbody2d keep its force and move at a consistent speed? 2 Answers

Rigidbody2D doesn't move 1 Answer

Simple moving ball script 1 Answer

2D Faux Gravity using a Point Effector? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges