TransWikia.com

How to fix the vibration while colliding?

Game Development Asked on January 13, 2021

I’m making a 2D platformer with Unity. My game has a problem:

The player vibrates when it collides with something. It seems like it’s 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.

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

    

One Answer

Your calling void AccelerateTo(Vector2 desiredVelocity) in your fixedUpdate().

You should not use Time.deltaTime in a fixedUpdate - you need to alter this code.

var acceleration = deltaV / Time.deltaTime;

When you use Time.DeltaTime in fixedUpdate you do not allow it to work properly. FixedUpdate has the frequency of the physics system and is called every fixed frame-rate frame.

Correct answer by Chris Norman on January 13, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP