Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

How to fix the player flying into the sky after applying velocity on the y to him.

Discussion in '2D' started by RgClube, Sep 29, 2020.

  1. RgClube

    RgClube

    Joined:
    Dec 24, 2019
    Posts:
    19
    Hi guys, I want to kill enemies by jumping on them something similar to mario, but with enemies that do not die after the first jump. With this implementation, I ran into several problems, first my player flies into the stratosphere after touching an enemy, the second is that the enemies take more damage than they should (continuing to fall the player continues to inflict damage). By the way, I managed to solve each of these problems, but separately. In the attached version of the code, only the problem of damage is solved, but not with flying. Thank you in advance.
    Code (CSharp):
    1. MonoBehaviour
    2. {
    3.    [SerializeField]
    4.    private LayerMask whatIsDamageable;
    5.    [SerializeField]
    6.    private float stunAmount = 3;
    7.    [SerializeField]
    8.    private Transform stampPosition;
    9.    [SerializeField]
    10.    private float attack1Damage;
    11.    [SerializeField]
    12.    private float jumpForce = 10;
    13.    public float attack1Radius = 5f;
    14.    private AttackDetails attackDetails;
    15.    private float currentHealth;
    16.    private float health = 100;
    17.    private GameObject player;
    18.    Rigidbody2D rb;
    19.    Collider2D detectedObject;
    20.    private float timeBetweenStamp = 1f;
    21.    private bool isEnemyDetected;
    22.    private bool playerJumpAfterHitEnemy;
    23.    private bool playerFinishedHisJump;
    24.    PlayerMovement playerMovement;
    25.    //animation
    26.    private void Update()
    27.    {
    28.        if(rb.velocity.y < 0){
    29.        CheckAttackHitBox();
    30.        }
    31.      
    32.        ifStampedEnemy();
    33.      
    34.      
    35.    }
    36.    private void Start()
    37.    {
    38.        rb = GetComponent<Rigidbody2D>();
    39.        playerMovement = GetComponent<PlayerMovement>();
    40.        currentHealth = health;
    41.        player = GameObject.Find("Player");
    42.    }
    43.  
    44.    private void CheckAttackHitBox()
    45.    {
    46.        isEnemyDetected = Physics2D.OverlapCircle(stampPosition.position, attack1Radius, whatIsDamageable);
    47.        detectedObject = Physics2D.OverlapCircle(stampPosition.position, attack1Radius, whatIsDamageable);
    48.        attackDetails.damageAmount = attack1Damage;
    49.        attackDetails.position = transform.position;
    50.        attackDetails.stunDamageAmount = stunAmount;
    51.        if(detectedObject != null && isEnemyDetected == true){
    52.        detectedObject.transform.parent.SendMessage("Damage", attackDetails);
    53.        timeBetweenStamp -= Time.deltaTime;
    54.        }
    55.        if(timeBetweenStamp <=0)
    56.        {
    57.            isEnemyDetected = false;
    58.            timeBetweenStamp = 2f;
    59.        }
    60.            // sendmeesage func is used to call a specific func in a script of an object
    61.            //Instantiate hit particle
    62.      
    63.    }
    64.    private void ifStampedEnemy()
    65.    {
    66.        if(isEnemyDetected)
    67.        {
    68.            rb.velocity = new Vector2 (rb.velocity.x , jumpForce);
    69.        }
    70.    }
    71.    private void Damage(AttackDetails attackDetails)
    72.    {
    73.        currentHealth -= attackDetails.damageAmount;
    74.    }
    75.    private void OnDrawGizmos()
    76.    {
    77.        Gizmos.DrawWireSphere(stampPosition.position, attack1Radius);
    78.    }
    79. }
     
  2. DiegoDePalacio

    DiegoDePalacio

    Unity Technologies

    Joined:
    Oct 28, 2009
    Posts:
    428
    Hi @RgClube,

    From what I see in your script, you're changing the rigidbody velocity directly in the line:
    rb.velocity = new Vector2 (rb.velocity.x , jumpForce);


    In most cases, you should not modify the velocity directly, as this can result in unrealistic behavior, you should use AddForce instead.

    You can find more information about it here: https://docs.unity3d.com/ScriptReference/Rigidbody-velocity.html

    My recommendation for you is to do the Unity's official 2D Physics tutorial: https://learn.unity.com/tutorial/2d-physics


    Good luck with it!
     
    RgClube likes this.
unityunity