Я работаю над 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#
Место общения программистов C#
1712731408
Anonymous
Я работаю над 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);
// }
// }
//}
}
Подробнее здесь: [url]https://stackoverflow.com/questions/78301347/is-there-a-way-to-make-a-transforming-platform-do-damage-in-unity[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия