How to fix the player/collision glitch?
I have a player who needs to push boxes around the 2d platform. The boxes have rigidbody2d and box collider 2d applied. Whenever the player pushes the box it glitches and jumps. I understand that it has something to do with the force/physics, I just don't know how to fix it. This is my code for the player:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
private Rigidbody2D myRigidBody;
private Animator myAnimator;
[SerializeField] //enables to control the object from unity
private float movementSpeed;
private bool facingRight;
[SerializeField]
private Transform[] groundPoints;
[SerializeField]
private float groundRadius;
[SerializeField]
private LayerMask whatIsGround;
private bool isGrounded;
[SerializeField]
//private bool airControl;
private bool jump;
[SerializeField]
private float jumpForce;
void Start()
{
facingRight = true;
myRigidBody = GetComponent<Rigidbody2D>();
myAnimator = GetComponent<Animator>();
}
private void Update()
{
HandleInput();
}
void FixedUpdate()
{
float horizontal = Input.GetAxis("Horizontal");
isGrounded = IsGrounded();
HandleMovement(horizontal);
Flip(horizontal);
HandleLayers();
ResetValues();
}
private void HandleInput()
{
if (Input.GetKeyDown(KeyCode.Space))
{
jump = true;
}
}
private void HandleMovement(float horizontal)
{
if (myRigidBody.velocity.y < 0)
{
myAnimator.SetBool("land", true);
}
if (isGrounded && jump)
{
isGrounded = false;
myRigidBody.AddForce(new Vector2(0, jumpForce));
myAnimator.SetTrigger("jump");
}
myRigidBody.velocity = new Vector2(horizontal * movementSpeed, myRigidBody.velocity.y);
myAnimator.SetFloat("speed", Mathf.Abs(horizontal));
}
private void Flip(float horizontal)
{
if (horizontal > 0 && !facingRight || horizontal<0 && facingRight)
{
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
private void ResetValues()
{
jump = false;
}
private bool IsGrounded()
{
if (myRigidBody.velocity.y <= 0)
{
foreach(Transform point in groundPoints)
{
Collider2D[] colliders = Physics2D.OverlapCircleAll(point.position, groundRadius, whatIsGround);
for(int i = 0; i < colliders.Length; i++)
{
if (colliders[i].gameObject != gameObject)
{
myAnimator.ResetTrigger("jump");
myAnimator.SetBool("land", false);
return true; //colliding
}
}
}
}
return false;
}
private void HandleLayers()
{
if (!isGrounded)
{
myAnimator.SetLayerWeight(1, 1);
}
else myAnimator.SetLayerWeight(1, 0);
}
}
Yes, In the IsGrounded() function... It returns true.
But as its never specified that 'Ground' (terrain etc) is not checked in the collision, wouldnt this condition become true will all collisions? I havent dissected the code, Im just glancing. Appears this may be handled by layermask? Can you be sure the Layer$$anonymous$$ask is correct?
In Unity I set ground to be everything, and I made the boolien isGrounded to be a serialized field just to check if it does turn true and it does, if my Layermask isn't correct then I'm not really sure how to fix it. I looked at it and it seems good. Plus, I'm not too good at coding so sorry if I dont completley understand you.