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.

Question Sprites does not collide properly

Discussion in 'Getting Started' started by Frostysh, Jul 26, 2022.

  1. Frostysh

    Frostysh

    Joined:
    Jul 11, 2022
    Posts:
    66
    Me following this instructions: World Interactions - Damage Zones and Enemies - Unity Learn, and it is my player-sprite script program.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Idiotic_Ruby_Controller : MonoBehaviour
    6. {
    7.     public int maxHealth = 5;
    8.    
    9.     public float time_invincible = 2.0f;
    10.  
    11.     public float maxSpeed = 3.0f;
    12.    
    13.     public int health { get { return currentHealth; }}
    14.  
    15.     int currentHealth;
    16.    
    17.     bool is_invincible;
    18.     float invincible_timer;
    19.  
    20.     Rigidbody2D rigidbody2d;
    21.     float horizontal;
    22.     float vertical;
    23.  
    24.     // Start is called before the first frame update
    25.     void Start()
    26.     {
    27.         rigidbody2d = GetComponent<Rigidbody2D>();
    28.         currentHealth = maxHealth;
    29.     }
    30.  
    31.     // Update is called once per frame
    32.     void Update()
    33.     {
    34.         horizontal = Input.GetAxis("Horizontal");
    35.         vertical = Input.GetAxis("Vertical");
    36.        
    37.         if (is_invincible)
    38.         {
    39.             invincible_timer -= Time.deltaTime;
    40.             if (invincible_timer < 0)
    41.                 is_invincible = false;
    42.         }
    43.     }
    44.  
    45.     void FixedUpdate()
    46.     {
    47.         Vector2 position = rigidbody2d.position;
    48.         position.x = position.x + maxSpeed*horizontal*Time.deltaTime;
    49.         position.y = position.y + maxSpeed*vertical*Time.deltaTime;
    50.        
    51.         rigidbody2d.MovePosition(position);
    52.     }
    53.    
    54.     public void ChangeHealth(int amount)
    55.     {
    56.         if (amount < 0)
    57.         {
    58.             if (is_invincible)
    59.                 return;
    60.  
    61.             is_invincible = true;
    62.             invincible_timer = time_invincible;
    63.         }
    64.             currentHealth = Mathf.Clamp(currentHealth + amount, 0, maxHealth);
    65.             Debug.Log(currentHealth + "/" + maxHealth);
    66.     }
    67. }
    And this is enemy sprite of which player should collide, script I mean.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class EnemyController : MonoBehaviour
    6. {
    7.     public float speed;
    8.     public float changeTime = 3.0f;
    9.     public bool vertical;
    10.    
    11.     Rigidbody2D rigidbody2D;
    12.     float timer;
    13.     int direction = 1;
    14.    
    15.     // Start is called before the first frame update
    16.     void Start()
    17.     {
    18.         rigidbody2D = GetComponent<Rigidbody2D>();
    19.         timer = changeTime;
    20.     }
    21.    
    22.     void Update()
    23.     {
    24.         timer -= Time.deltaTime;
    25.        
    26.         if (timer < 0)
    27.         {
    28.             direction = -direction;
    29.             timer = changeTime;
    30.         }
    31.     }
    32.    
    33.     void FixedUpdate()
    34.     {
    35.         Vector2 position = rigidbody2D.position;
    36.        
    37.         if (vertical)
    38.         {
    39.             position.y = position.y + Time.deltaTime*speed*direction;
    40.         }
    41.         else
    42.         {
    43.             position.x = position.x + Time.deltaTime*speed*direction;
    44.         }
    45.        
    46.         rigidbody2D.MovePosition(position);
    47.  
    48.     void OnCollisonEnter2D(Collision2D other)
    49.     {
    50.         Idiotic_Ruby_Controller player = other.gameObject.GetComponent<Idiotic_Ruby_Controller>();
    51.        
    52.         if (player != null)
    53.         {
    54.             player.ChangeHealth(-1);
    55.         }
    56.     }
    57.        
    58.     }
    59. }
    But in reality collisions almost no working. I have made two prefabs so called with enemies, with different speed and color each one. When I am trying to collide them in game mode, only the one of ten collisions is damaging Health of player, which is weird...



    There should be text about damage, but no such things during 90% of collisions. What went wrong?