Player Shakes When Colliding With Objects (Powerups)

Hello everyone! I am working on making an auto runner game. I have a Powerup called “Shield” that gives the Player another point of health. It is supposed to disappear when the Player touches it. The code works, and it does both of those things, however I’ve noticed while playing that the Player also slows down/appears to shake a little bit when it comes into contact with the Powerups, and also after jumping off of enemies’ heads if it connects with the enemy in a weird way. Like if it jumps on the enemies’ head towards the end of the sprite, for example.

I think it MAY have to do with the Translate code I am using to make the Player automatically move right. However, I also have a “Speed” variable that I don’t want to remove because I am going to have objects later that will increase that speed. It could also have to do with the Player’s Rigidbody, because the Player also does this when they collide with Enemies. I also have the Player’s gravity set to 6, and they are set to Dynamic in their Rigidbody2D. The Powerups do not use a Rigidbody, and only use a capsule collider. I figured they wouldn’t need a rigidbody because they are just powerups and won’t be moving anywhere. The enemies however, DO have Rigidbody2D.

Here is my code for the Player’s Movement:

public class Movement : MonoBehaviour

{



public Rigidbody2D player;

// Speed that the Player moves forward

public float speed = 1.0f;

// Jump Speed Variable. Changing makes Player jump higher.

public float jumpSpeed = 15f;



//Ground Check Variables

public UnityEngine.Transform groundCheck;

public float groundCheckRadius;

public LayerMask groundLayer;

private bool isTouchingGround;



// Animator Variable

public Animator animator;



private void Start()

{

player = GetComponent<Rigidbody2D>();

}

// Update is called once per frame

void Update()

{

// Ground Check

isTouchingGround = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, groundLayer);



// Makes the Player constantly move forward to the right

// Speed * 15 makes the player move faster

transform.Translate(Vector3.right * (speed * 12) * Time.deltaTime);



// Make Player Jump

if (Input.GetButtonDown("Jump") && isTouchingGround)

{

player.velocity = new Vector2(player.velocity.x, jumpSpeed);

}

}

}

Here is my code for picking up the Shield Powerup:

public class ShieldCollect : MonoBehaviour

{

public int addhealth;

// Calls the PlayerHealth script

public PlayerHealth playerHealth;

private void OnCollisionEnter2D(Collision2D collision)

{ // If the Player collides with the Shield powerup

if (collision.gameObject.tag == "Player")

{

// Increase Player's Health by 1

playerHealth.HealthUp(addhealth);

// Remove the Shield Powerup from the level

Destroy(gameObject);

}

}

}

And finally the code for jumping on Enemies.

public class EnemyStomp : MonoBehaviour
{
    public Rigidbody2D player;
    public float jumpSpeed = 15f;
    private void OnCollisionEnter2D(Collision2D collision)
    {
        // When Player's foot collides with Enemies that have the "WeakPoint" tag
       if(collision.gameObject.tag == "WeakPoint")
        {
            // Makes the Player bounce off of the enemy when they stomp on them.
            // Reuses the same code as whent the Player Jumps.
            player.velocity = new Vector2(player.velocity.x, jumpSpeed);
            // Destroy the Enemy when Player's foot hits them.
            Destroy(collision.gameObject);
        }
    }
}

Your power up’s colliders should be triggers, and your OnCollision methods can be changed to OnTrigger methods.

When I set the power ups to triggers, I just pass through them when colliding. So if I set it to OnTrigger that will work?

Yes that’s the point of those Unity messages.

Triggers are colliders that don’t block collisions, but will call OnTriggerEnter and similar on components on the same game object (and parent game objects too, I believe).

Just tested it out on both the Powerup and the collision with the Enemy, and it works SO WELL! Thanks so much! The game feels a lot smoother now. I appreciate it!

1 Like

Sweet, thanks! Whole issue is fixed now!

1 Like