У меня есть кинематическое твердое тело и коллайдер 2d, прикрепленный к этому объекту. Я пытаюсь заставить моего персонажа запустить сигнальную ракету, чтобы отвлечь ракету от игрока. Вот мой код:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class flareShoot : MonoBehaviour
{
public GameObject flarePrefab; // Prefab for the flare
public Transform flareSpawnPoint; // Spawn point for the flare on the player
private GameObject currentFlare; // Current flare instance
public float fireForce = 10f; // Force to apply when firing the flare
void Start()
{
SpawnFlare();
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space) && currentFlare != null)
{
FireFlare();
}
}
void SpawnFlare()
{
currentFlare = Instantiate(flarePrefab, flareSpawnPoint.position, flareSpawnPoint.rotation, flareSpawnPoint);
currentFlare.GetComponent().isKinematic = true; // Disable physics while attached
}
void FireFlare()
{
currentFlare.transform.SetParent(null); // Detach from player
Rigidbody2D rb = currentFlare.GetComponent();
rb.isKinematic = false; // Enable physics
rb.gravityScale = 1; // Enable gravity if needed
rb.AddForce(flareSpawnPoint.up * fireForce, ForceMode2D.Impulse); // Apply force to fire
currentFlare = null; // Reset the reference to the flare
}
}
Подробнее здесь: https://stackoverflow.com/questions/791 ... from-a-gun
Как я могу изменить свой код, чтобы сделать запуск ракеты из пистолета? ⇐ C#
Место общения программистов C#
1731376949
Anonymous
У меня есть кинематическое твердое тело и коллайдер 2d, прикрепленный к этому объекту. Я пытаюсь заставить моего персонажа запустить сигнальную ракету, чтобы отвлечь ракету от игрока. Вот мой код:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class flareShoot : MonoBehaviour
{
public GameObject flarePrefab; // Prefab for the flare
public Transform flareSpawnPoint; // Spawn point for the flare on the player
private GameObject currentFlare; // Current flare instance
public float fireForce = 10f; // Force to apply when firing the flare
void Start()
{
SpawnFlare();
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space) && currentFlare != null)
{
FireFlare();
}
}
void SpawnFlare()
{
currentFlare = Instantiate(flarePrefab, flareSpawnPoint.position, flareSpawnPoint.rotation, flareSpawnPoint);
currentFlare.GetComponent().isKinematic = true; // Disable physics while attached
}
void FireFlare()
{
currentFlare.transform.SetParent(null); // Detach from player
Rigidbody2D rb = currentFlare.GetComponent();
rb.isKinematic = false; // Enable physics
rb.gravityScale = 1; // Enable gravity if needed
rb.AddForce(flareSpawnPoint.up * fireForce, ForceMode2D.Impulse); // Apply force to fire
currentFlare = null; // Reset the reference to the flare
}
}
Подробнее здесь: [url]https://stackoverflow.com/questions/79179593/how-can-i-change-my-code-to-make-a-flare-launch-from-a-gun[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия