Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Player Shakes When Colliding With Objects (Powerups)

Discussion in 'Editor & General Support' started by Ani311, Jul 6, 2023.

  1. Ani311

    Ani311

    Joined:
    May 29, 2023
    Posts:
    12
    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:

    Code (CSharp):
    1. public class Movement : MonoBehaviour
    2.  
    3. {
    4.  
    5.  
    6.  
    7. public Rigidbody2D player;
    8.  
    9. // Speed that the Player moves forward
    10.  
    11. public float speed = 1.0f;
    12.  
    13. // Jump Speed Variable. Changing makes Player jump higher.
    14.  
    15. public float jumpSpeed = 15f;
    16.  
    17.  
    18.  
    19. //Ground Check Variables
    20.  
    21. public UnityEngine.Transform groundCheck;
    22.  
    23. public float groundCheckRadius;
    24.  
    25. public LayerMask groundLayer;
    26.  
    27. private bool isTouchingGround;
    28.  
    29.  
    30.  
    31. // Animator Variable
    32.  
    33. public Animator animator;
    34.  
    35.  
    36.  
    37. private void Start()
    38.  
    39. {
    40.  
    41. player = GetComponent<Rigidbody2D>();
    42.  
    43. }
    44.  
    45. // Update is called once per frame
    46.  
    47. void Update()
    48.  
    49. {
    50.  
    51. // Ground Check
    52.  
    53. isTouchingGround = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, groundLayer);
    54.  
    55.  
    56.  
    57. // Makes the Player constantly move forward to the right
    58.  
    59. // Speed * 15 makes the player move faster
    60.  
    61. transform.Translate(Vector3.right * (speed * 12) * Time.deltaTime);
    62.  
    63.  
    64.  
    65. // Make Player Jump
    66.  
    67. if (Input.GetButtonDown("Jump") && isTouchingGround)
    68.  
    69. {
    70.  
    71. player.velocity = new Vector2(player.velocity.x, jumpSpeed);
    72.  
    73. }
    74.  
    75. }
    76.  
    77. }
    Here is my code for picking up the Shield Powerup:
    Code (CSharp):
    1. public class ShieldCollect : MonoBehaviour
    2.  
    3. {
    4.  
    5. public int addhealth;
    6.  
    7. // Calls the PlayerHealth script
    8.  
    9. public PlayerHealth playerHealth;
    10.  
    11. private void OnCollisionEnter2D(Collision2D collision)
    12.  
    13. { // If the Player collides with the Shield powerup
    14.  
    15. if (collision.gameObject.tag == "Player")
    16.  
    17. {
    18.  
    19. // Increase Player's Health by 1
    20.  
    21. playerHealth.HealthUp(addhealth);
    22.  
    23. // Remove the Shield Powerup from the level
    24.  
    25. Destroy(gameObject);
    26.  
    27. }
    28.  
    29. }
    30.  
    31. }
    And finally the code for jumping on Enemies.

    Code (CSharp):
    1. public class EnemyStomp : MonoBehaviour
    2. {
    3.     public Rigidbody2D player;
    4.     public float jumpSpeed = 15f;
    5.     private void OnCollisionEnter2D(Collision2D collision)
    6.     {
    7.         // When Player's foot collides with Enemies that have the "WeakPoint" tag
    8.        if(collision.gameObject.tag == "WeakPoint")
    9.         {
    10.             // Makes the Player bounce off of the enemy when they stomp on them.
    11.             // Reuses the same code as whent the Player Jumps.
    12.             player.velocity = new Vector2(player.velocity.x, jumpSpeed);
    13.             // Destroy the Enemy when Player's foot hits them.
    14.             Destroy(collision.gameObject);
    15.         }
    16.     }
    17. }
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    Your power up's colliders should be triggers, and your OnCollision methods can be changed to OnTrigger methods.
     
  3. Ani311

    Ani311

    Joined:
    May 29, 2023
    Posts:
    12
    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?
     
    Last edited: Jul 6, 2023
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    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).
     
  5. Ani311

    Ani311

    Joined:
    May 29, 2023
    Posts:
    12
    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!
     
    spiney199 likes this.
  6. Ani311

    Ani311

    Joined:
    May 29, 2023
    Posts:
    12
    Sweet, thanks! Whole issue is fixed now!
     
    spiney199 likes this.