Здравствуйте, имеется вопрос по скриптингу в Unity. На сцене находится персонаж с точкой спавна для объекта (TNT)

Персонаж должен кидать ТNT. У него имеется собственный скрипт.
Cкрипт tnt:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TNT : MonoBehaviour {
public int damage;
public GameObject boomEffect;
private HpPlayer hp;
void Start()
{
hp = GameObject.FindGameObjectWithTag("Player").GetComponent<HpPlayer>();
}
void OnTriggerEnter2D (Collider2D col)
{
if (!col.CompareTag("Enemy"))
{
gameObject.SetActive(false);
GameObject ps = Instantiate(boomEffect, transform.position, Quaternion.identity);
Destroy(ps, 1);
if(col.CompareTag("Player"))
hp.health -= damage;
}
}
}
Cкрипт персонажа:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Throw : MonoBehaviour {
public GameObject tnt;
public Transform spawnPoint;
public static GameObject gm;
public Vector2 direction;
public float speed;
public float time;
private Rigidbody2D tntRb;
private Animator animator;
void Start ()
{
animator = GetComponent<Animator>();
gm = Instantiate(tnt);
spawnPoint = GameObject.FindGameObjectWithTag("Point").GetComponent<Transform>();
tntRb = gm.GetComponent<Rigidbody2D>();
}
void Update ()
{
StartCoroutine(TakeAndThrow());
}
IEnumerator TakeAndThrow()
{
animator.SetBool("Throw", false);
yield return new WaitForSeconds(1f);
gm.transform.position = spawnPoint.position;
gm.SetActive(true);
yield return new WaitForSeconds(1f);
animator.SetBool("Throw", false);
yield return new WaitForSeconds(0.5f);
tntRb.simulated = true;
tntRb.AddForce(direction * speed, ForceMode2D.Impulse);
yield return new WaitForSeconds(time);
}
}
Собственно скрипты работают, как-то странно. Во первых: они отрабатывают 1 раз, а во вторых: кидок происходит вниз не по вектору.
Спасибо всем кто поможет!