У меня есть кинематическое твердое тело и коллайдер 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