Reputation: 2068
Update: after go further with this book, I get the reason. It is the box colliders limitation of Unity it said in book. So I think we could close this ticket.
What said in the book:
"Right now, if you’re using box colliders on your tiles and on your player character, you might find that you occasionally get stuck on things that you shouldn’t—your character may stop moving and press against thin air. The problem normally occurs at the vertices between the closely positioned tiles, and unfortunately, it seems that this is a slight issue with Unity itself rather than anything you can fix (apparently, it was introduced in 4.3.1 and has yet to be resolved as of the time of writing). If you do encounter this problem, just try using a polygon collider for Squarey himself, changing the shape slightly by clicking Edit Collider, and then creating a small bump in the outline as in Figure 5-9 so that you can “slide” over these imagined obstacles."
Following a book, I create a simple game using Unity 2D. It includes a GameObject
named 'Player' which could do some simply action, move to left/right/jump when pressing leftArrow/rightArrow/Space key of my PC. And also some tiles as platform ground.
I found a problem that when the game is played, firstly the Player will drop on the platform then when I press Right Arrow Key, it has no effect. But if I press Left Key or Space, the Player will go left or jump then the Right Click will take effect...
This problem takes me for hours!
Searching the internet but no answer found. One clue I found is that when I uncheck the Freeze Rotation
option under RigidBody2D
, then right move worked but I don't know why yet.
Here is my code, guys your help will be appreciated!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
public Rigidbody2D rb;
public int moveSpeed;
public int jumppower;
public Transform groundCheck;
public float groundCheckRadius;
public LayerMask whatIsGround;
private bool onGround;
// Use this for initialization
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
private void FixedUpdate()
{
onGround = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius,
whatIsGround);
}
// Update is called once per frame
void Update()
{
if (Input.GetKey(KeyCode.LeftArrow))
{
rb.velocity = new Vector2(-moveSpeed, rb.velocity.y);
}
if (Input.GetKey(KeyCode.RightArrow))
{
rb.velocity = new Vector2(moveSpeed, rb.velocity.y);
}
if (Input.GetKey(KeyCode.Space) && onGround)
{
rb.velocity = new Vector2(rb.velocity.x, jumppower);
}
}
}
Upvotes: 0
Views: 348
Reputation: 2068
OKay, let me close this question. The reason why the Player is occasionally stop during the moving is because the limitation of Box Collider. The workaround could be using polygon collider instead and change the shapes, that is what is said on the book.
Of course I have a try on my project, it works actually. Thanks guys, the case is closed now and I will learn Unity step by step since the interest of games!
Upvotes: 1