Я работаю над 2D-платформером в Unity для школьного проекта, и для одной из механик у нас есть своего рода нагревательный элемент, который включается и выключается через определенное время. В настоящее время у меня есть часть включения и выключения из обрывков кода, которые я нашел в Интернете, но я не могу заставить ее нанести вред моему персонажу.
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.InputSystem.Controls;
public class heatPlatforms : MonoBehaviour
{
public List spritesToSwap; // contains all the sprites to loop through
public float Speed = 0f;
IEnumerator SwapSprites()
{
int spriteIndex = 0;
while (true)
{
gameObject.GetComponent().sprite = spritesToSwap[spriteIndex];
// ^ replace the sprite
spriteIndex++;
if (spriteIndex == spritesToSwap.Count) spriteIndex = 0;
// ^ loop back to first sprite
yield return new WaitForSeconds(Speed); // adjust this time as desired
}
}
// Start is called before the first frame update
void Start()
{
//Starts the loop
StartCoroutine(SwapSprites());
}
// Update is called once per frame
void Update()
{
}
void OnCollisionEnter2D(Collision2D other)
{
//when character hits the proper sprite, then you take damage.
PlayerCharacter player = other.gameObject.GetComponent();
//sprite that does damage should be second sprite in the array.
if (spritesToSwap.Count == 1)
{
if (player != null)
{
player.ChangeHealth(-1);
}
}
}
//Can not do the code below because triggers have nothing for character to stand on. Will only go through the sprite.
//void OnTriggerEnter2D(Collider2D other)
//{
// PlayerCharacter controller = other.GetComponent();
// if (spritesToSwap.Count == 1)
// {
// if (controller != null)
// {
// controller.ChangeHealth(-1);
// }
// }
//}
}
Подробнее здесь: https://stackoverflow.com/questions/783 ... e-in-unity
Есть ли способ заставить платформу-трансформер наносить урон в единстве? ⇐ C#
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение