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

Question Question about moving player to trigger death animation

Discussion in '2D' started by VidaTheHydroxZ, Jun 19, 2023.

  1. VidaTheHydroxZ

    VidaTheHydroxZ

    Joined:
    Jun 9, 2023
    Posts:
    2
    Hello.

    I have a question regarding my 2D Platformer game which I am currently creating.

    When my player jumps on an object like a "Saw" or "SpikedBall" or any kind of game object which would make him die, or even if he falls down from the platform he dies. I put a death animation on my player and everything works fine except for one small issue. When a "Player" for example falls off a platform and you don't press any movement keys he stands there and he does not die, rather the moment you start moving him only then does it trigger his death and restarts the level.
    Also, player becomes a static object and he cannot move the moment he touches anything that would make him die, but the death animation triggers only when you press the movement keys. That happens also when you jump on traps such as a "Saw" or "SpikedBall". If you jump on a trap and you don't press any movement keys, nothing will happen and the player will not die until you touch movement keys.
    This is the script I wrote for Player death:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class PlayerLife : MonoBehaviour
    7. {
    8.     private Rigidbody2D rb;
    9.     private Animator anim;
    10.     [SerializeField]private AudioSource Death;
    11.     private Vector3 respawnPoint;
    12.  
    13.     private void Start()
    14.     {
    15.         rb = GetComponent<Rigidbody2D>();
    16.         anim = GetComponent<Animator>();
    17.         respawnPoint = transform.position;
    18.     }
    19.  
    20.     private void OnCollisionEnter2D(Collision2D collision)
    21.     {
    22.         if (collision.gameObject.CompareTag("SpikedBall"))
    23.         {
    24.             Die();
    25.             Death.Play();
    26.         }
    27.         if (collision.gameObject.CompareTag("Saw"))
    28.         {
    29.             Die();
    30.             Death.Play();
    31.         }
    32.         if (collision.gameObject.CompareTag("FallDetector"))
    33.         {
    34.             Die();
    35.             Death.Play();
    36.         }
    37.      
    38.     }
    39.  
    40.         private void Die()
    41.     {
    42.         rb.bodyType = RigidbodyType2D.Static;
    43.         anim.SetTrigger("death");
    44.     }
    45.  
    46.     private void OnTriggerEnter2D(Collider2D collision)
    47.     {
    48.         if (collision.gameObject.CompareTag("Checkpoint"))
    49.         {
    50.             respawnPoint = transform.position;
    51.         }
    52.     }
    53.  
    54.     private void RestartLevel()
    55.     {
    56.         SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    57.     }
    58.  
    59. }
    Thank you very much for your help in advance.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    I'm guessing you are not moving the player correctly but you didn't post that code.

    With Physics (or Physics2D), never manipulate the Transform directly. If you manipulate the Transform directly, you are bypassing the physics system and you can reasonably expect glitching and missed collisions and other physics mayhem.

    This means you may not change transform.position, transform.rotation, you may not call transform.Translate(), transform.Rotate() or other such methods, and also transform.localScale is off limits. You also cannot set rigidbody.position or rigidbody.rotation directly. These ALL bypass physics.

    Always use the .MovePosition() and .MoveRotation() methods on the Rigidbody (or Rigidbody2D) instance in order to move or rotate things. Doing this keeps the physics system informed about what is going on.

    https://forum.unity.com/threads/col...-unity-physic-rigidbody.1216875/#post-7763061

    https://forum.unity.com/threads/oncollisionenter2d-not-being-called.1266563/#post-8044121
     
    VidaTheHydroxZ likes this.
  3. VidaTheHydroxZ

    VidaTheHydroxZ

    Joined:
    Jun 9, 2023
    Posts:
    2
    Thank you very much for your answer. Unfortunately I didn't work on the project for some time but I'm back on track now.

    I am using the rigid body .velocity method for player movement.

    Please find the code for player movement below:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using UnityEngine.UIElements;
    4.  
    5. public class PlayerMovement : MonoBehaviour
    6. {
    7.  
    8.     private Rigidbody2D rb;
    9.     private BoxCollider2D coll;
    10.     private SpriteRenderer sprite;
    11.     private Animator anim;
    12.    
    13.  
    14.     [SerializeField] private LayerMask jumpableGround;
    15.  
    16.     private float PlayerStamina = 10f;
    17.     [SerializeField] private Text Stamina;
    18.  
    19.     private float dirX = 0f;
    20.     [SerializeField] private float moveSpeed = 7f;
    21.     [SerializeField] private float jumpForce = 14f;
    22.    
    23.     private enum MovementState { idle, running, jumping, falling }
    24.  
    25.     [SerializeField] private AudioSource jumpSoundEffect;
    26.  
    27.     // Start is called before the first frame update
    28.     private void Start()
    29.     {
    30.         rb = GetComponent<Rigidbody2D>();
    31.         coll = GetComponent<BoxCollider2D>();
    32.         sprite = GetComponent<SpriteRenderer>();
    33.         anim = GetComponent<Animator>();
    34.  
    35.     // Update is called once per frame
    36.     private void Update()
    37.     {
    38.      
    39.         // Code for Windows
    40.         dirX = Input.GetAxisRaw("Horizontal");
    41.         rb.velocity = new Vector2(dirX * moveSpeed, rb.velocity.y);
    42.         if ((PlayerStamina > 2f && Input.GetButtonDown("Jump")) && IsGrounded())
    43.         {
    44.             PlayerStamina -= 2f;
    45.             jumpSoundEffect.Play();
    46.             rb.velocity = new Vector2(rb.velocity.x, jumpForce);
    47.         }
    48.        
    49.         if (PlayerStamina < 0f && Input.GetButtonDown("Jump") == true)
    50.         {
    51.            
    52.         }
    53.        
    54.         PlayerStamina += Time.deltaTime;
    55.         Stamina.text = "Stamina: " + Mathf.Round(PlayerStamina);
    56.         Debug.Log(jumpForce);
    57.        
    58.         UpdateAnimationState();
    59.        
    60.      
    61.     }
    62.  
    63.  
    64.  
    65.     private void UpdateAnimationState()
    66.     {
    67.        
    68.         MovementState state;
    69.         // Code for Windows
    70.         if (dirX > 0f)
    71.         {
    72.             state = MovementState.running;
    73.             sprite.flipX = false;
    74.         }
    75.         else if (dirX < 0f)
    76.         {
    77.             state = MovementState.running;
    78.             sprite.flipX = true;
    79.         }
    80.         else
    81.         {
    82.             state = MovementState.idle;
    83.         }
    84.        
    85.         if (rb.velocity.y > .1f)
    86.         {
    87.             state = MovementState.jumping;
    88.         }
    89.         else if (rb.velocity.y < -.1f)
    90.         {
    91.             state = MovementState.falling;
    92.         }
    93.  
    94.         anim.SetInteger("State", (int)state);
    95.     }
    96.  
    97.     private bool IsGrounded()
    98.     {
    99.         return Physics2D.BoxCast(coll.bounds.center, coll.bounds.size, 0f, Vector2.down, .1f, jumpableGround);
    100.     }
    101.    
    102. }
    103.  
     
    Kurt-Dekker likes this.