0
\$\begingroup\$
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Makarov : MonoBehaviour
{
    public GameObject Bullet;

    public float BulletSpeed;

    public Transform ShootPoint;

    // Update is called once per frame
    void Update()
    {
        if(Input.GetMouseButtonDown(0))
        {
            shoot();
        }
    }

    void shoot()
    {
        GameObject BulletIns = Instantiate(Bullet, ShootPoint.position, ShootPoint.rotation);
        BulletIns.GetComponent<Rigidbody2D>().AddForce(BulletIns.transform.forward * BulletSpeed);
    }
}

When I shoot the bullets just stop right in front if the gun. This is for a top down shooting.

\$\endgroup\$
1
  • \$\begingroup\$ Your variable says BulletSpeed but you're applying it as a force, not as a speed. That's likely to make it vastly less effective than you'd expect, and you'd have to compensate by putting much higher numbers in to overcome that mismatch. \$\endgroup\$
    – DMGregory
    Nov 26 '20 at 22:15
0
\$\begingroup\$
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Makarov : MonoBehaviour
{
    public GameObject Bullet;

    public Transform ShootPoint;

    public float bulletSpeed = 20f;

    // Update is called once per frame
    void Update()
    {
        if(Input.GetMouseButtonDown(0))
        {
            Shoot();
        }
    }

    void Shoot()
    {
        GameObject bullet = Instantiate(Bullet, ShootPoint.position, firePoint.rotation);
        Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();
        rb.AddForce(ShootPoint.up * bulletSpeed, ForceMode2D.Impulse);
        
    }
}

This works!

\$\endgroup\$
1
  • \$\begingroup\$ You're now applying your speed as an impulse, still not a speed. Try rb.AddForce(ShootPoint.up * bulletSpeed * rb.mass, ForceMode2D.Impulse), or more simply rb.velocity = ShootPoint.up * bulletSpeed \$\endgroup\$
    – DMGregory
    Nov 26 '20 at 22:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .