I have a question that I could not solve, as the title says I want to make a shot to any direction in 2D. One way I’m trying is for the bullet to move by force…
But what happens is that the bullet remains static and does not go to any direction, the bullet has a dynamic rigidbody. There is also the creation of a ray but this time I wish there was an object.
Now the bullet goes in one direction no matter what it does. What I did was change the bullet script to add direction and strength, as seen in the script; One problem I have is that the bullet is a prefab and I had to prefab the sight and muzzle of the weapon.
public class Bala : MonoBehaviour
{
public Rigidbody2D rb;
public Transform mira;
public Transform bocaArma;
public float fuerzaInicial;
public float tiempo;
private float t;
void Start()
{
t = tiempo;
}
void Update()
{
Vector2 direccion = (mira.position - bocaArma.position).normalized;
rb.AddForce(fuerzaInicial * direccion, ForceMode2D.Impulse);
if(t <= 0)
{
Destroy(gameObject);
}
else
{
t -= Time.deltaTime;
}
}
}
Another little thing, I do so that the bullet when colliding with the player or an enemy does not push him or transfer the force to him.
But that would give problems since it would not cover the entire “firing zone”? it would be better to add a force to it but my script must have some problem since it does not run as I wish?