![![alt text][1]][1]I'm making a topDown shooting game and I want to add an explosion. I use addforce to do the explosion push, and it works, except for the player. The player just teleports intead of moving from the explosion. I realy need some help and its realy buggin me. Thank you in advance.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class explosion : MonoBehaviour
{
public float fieldOfImpact;
public float force;
public GameObject thisGameObject;
public LayerMask layerToHit;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if(Input.GetKeyDown(KeyCode.E))
{
explode();
}
}
public void explode ()
{
Collider2D[] objects = Physics2D.OverlapCircleAll(thisGameObject.transform.position, fieldOfImpact, layerToHit);
foreach (Collider2D obj in objects)
{
Vector2 direction = obj.transform.position - thisGameObject.transform.position;
obj.GetComponent().AddForce(direction * force);
}
}
private void OnDrawGizmos()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(thisGameObject.transform.position, fieldOfImpact);
}
}
[1]: /storage/temp/169434-square.png
↧