Столкновение 2 раза с одним и тем же объектомC#

Место общения программистов C#
Anonymous
Столкновение 2 раза с одним и тем же объектом

Сообщение Anonymous »


I want to start by saying that I need some guidance, the first thing is that my player is colliding with the upgrade(Cube) object 2 times every time, so instead of receiving 2damage, I receive 4 damage. This is the colliding code:

public void OnTriggerEnter(Collider other) { if (other.CompareTag("Upgrade") && this != null) { Debug.Log("Collide:" + other.name); Upgrades upgradesScript = other.GetComponent(); if(upgradesScript != null) { Damage += upgradesScript.Damage; Health += upgradesScript.Health; Xspeed += upgradesScript.Xspeed; } } } And here is the upgrade script:

using System.Collections; using System.Collections.Generic; using UnityEngine; public class Upgrades : MonoBehaviour { public float Damage; //id 0 public float Xspeed; //id 1 public float Health; //id 2 public float itemID; public float stage = 1; void Start() { if(itemID == 0) { RandomDamage(); } else if(itemID == 1) { RandomXspeed(); } else { RandomHealth(); } } void Update() { } public void RandomDamage() { Damage = Random.Range(1 , 3 ); } public void RandomXspeed() { Xspeed = Random.Range(1 * stage, 3 * stage); } public void RandomHealth() { Health = Random.Range(5 , 10 ); } } This is from inspector:


Изображение


Now, if you could also tell me if I took the right approach, or what could I do to optimize my code more, regarding the switch instead of if's, I heard is better but I don't fully understand how it is better that's why I haven't used it. Thank you!


Источник: https://stackoverflow.com/questions/775 ... ame-object

Вернуться в «C#»