Player making "random jumps" while is moving at random places (Unity 2D)
Hi guys!
I'm having a little problem over here with Unity 2D. My player makes little jumps while it's moving. I think it was related to my 2D collider (it was a box for my player)... So i changed it for a CircleCollider2D
Right now my editor looks like this:
But while it's moving, it makes little jumps, something like it's "collisioning" with some parts of the floor. And it's completely random.
Here is the movement script I'm using for my player
public class PlayerController : MonoBehaviour
{
private Vector3 jump;
private Vector3 speed;
public float jumpForce = 2.0f;
public float speedAcceleration = 2.0f;
private bool isGrounded;
Rigidbody2D rb;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
jump = new Vector3(0.0f, jumpForce, 0.0f);
speed = new Vector3(speedAcceleration, 0.0f, 0.0f);
}
private void OnCollisionEnter2D(Collision2D collision)
{
if(!collision.gameObject.tag.Equals("Player"))
{
isGrounded = true;
}
}
private void FixedUpdate()
{
if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
{
rb.AddForce(jump * jumpForce, ForceMode2D.Impulse);
isGrounded = false;
}
if (Input.GetKey(KeyCode.D))
{
rb.AddForce(speed * speedAcceleration, ForceMode2D.Force);
}
else if (Input.GetKey(KeyCode.A))
{
rb.AddForce(speed * speedAcceleration * -1, ForceMode2D.Force);
}
}
// Update is called once per frame
void Update()
{
}
}
(The fixedUpdate code was before into the Update method, but I decided to change it to check if this was the cause of this "strange" thing that it's happening.
Hope to hear from you soon and receive a solution!
Your answer
Follow this Question
Related Questions
Problem with Jumping code in Unity 2D,problem with Jumping code in Unity 2D 0 Answers
Overlapping colliders with Raycasting in 2D 0 Answers
Checking OnCollisionEnter2D names with TilemapCollider2D 0 Answers
Moving forward seems to be specific to the original direction of the object being controlled 0 Answers