- Home /
How to fix the vibration while colliding and the suspension after jumping?
I'm making a 2D platformer. Currently, my game has two problems:
1) The player vibrates when it collides with something, it's like spinning or flipping very quickly. I used transform.position
in my code before but because of some kind of vibration I changed it to AddForce
but still I have that problem.
2) The jumping is not working properly. The player will be suspended in the air after the jumping and it can walk in that situation. This just lasts for a few seconds after each jump and then it returns to the ground but I know that this is not normal.
Thank you in advance.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerController : MonoBehaviour
{
[SerializeField] private float jumpSpeed;
public float maxSpeed = 5f;
public float maxAcceleration = 50f;
public float maxDeceleration = 100f;
[SerializeField] private LayerMask ground;
private PlayerActionControls playerActionControls;
private Rigidbody2D rb;
private PolygonCollider2D pol;
private Animator animator;
private Vector2 movementInput;
private void Awake()
{
playerActionControls = new PlayerActionControls();
rb = GetComponent<Rigidbody2D>();
pol = GetComponent<PolygonCollider2D>();
animator = GetComponent<Animator>();
}
private void OnEnable()
{
playerActionControls.Enable();
}
private void OnDisable()
{
playerActionControls.Disable();
}
void Start()
{
playerActionControls.Land.Jump.performed += ctx => Jump(ctx.ReadValue<float>());
}
private void Jump(float val)
{
if (val == 1 && IsGrounded())
{
rb.AddForce(new Vector2(0, jumpSpeed), ForceMode2D.Impulse);
}
}
private bool IsGrounded()
{
Vector2 topLeftPoint = transform.position;
topLeftPoint.x -= pol.bounds.extents.x;
topLeftPoint.y += pol.bounds.extents.y;
Vector2 bottomRightPoint = transform.position;
bottomRightPoint.x += pol.bounds.extents.x;
bottomRightPoint.y -= pol.bounds.extents.y;
return Physics2D.OverlapArea(topLeftPoint, bottomRightPoint, ground);
}
void FixedUpdate()
{
Vector2 movementInput = playerActionControls.Land.Move.ReadValue<Vector2>();
movementInput = Vector2.ClampMagnitude(movementInput, 1f);
Vector2 DesiredVelocity = movementInput * maxSpeed;
AccelerateTo(DesiredVelocity);
var scale = transform.localScale;
if(scale.x * rb.velocity.x < 0) {
scale.x *= -1;
transform.localScale = scale;
}
void AccelerateTo(Vector2 desiredVelocity) {
var deltaV = desiredVelocity - rb.velocity;
var acceleration = deltaV / Time.deltaTime;
float limit = maxAcceleration;
if (Vector3.Dot(rb.velocity, desiredVelocity) <= 0f)
limit = maxDeceleration;
acceleration = Vector2.ClampMagnitude(acceleration, limit);
Vector2 force = rb.mass * acceleration;
rb.AddForce(force);
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613021708im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
[RESOLVED] Pressing the "Z" key to make him go through certain terrain objects? 1 Answer
Jumping while moving on the x axis being weird 1 Answer
How can I detect a collision between two clones and delete them both? 1 Answer
Why isn't my player looking the other direction when I wall jump (please help I am desperate) 1 Answer
2D collisions in Unity 4.3? 1 Answer