How to make 2d shooting with instantiated bullets
I am making my first platforming game, and I am making shooting controls with multiple firing types. I have gotten to the point where if I press space, it will instantiate the bullet where I want it to (an empty called firepoint) but the bullets are not moving forward at all.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BulletController : MonoBehaviour {
public Transform firepoint;
public GameObject defaultShot;
public Rigidbody2D defaultRb;
public float defaultSpeed;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
if(Input.GetKeyDown("space"))
{
Instantiate (defaultShot, firepoint.position, firepoint.rotation);
defaultRb.velocity = transform.forward * defaultSpeed;
}
}
}
The reason most of my GO's, RB's, and floats are started with 'default' is because I would like to add multiple firing types in the future.
Any help on this topic would be much appreciated!
Your answer
![](https://koobas.hobune.stream/wayback/20220612184954im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to spawn a prefab at a duplicate objects location 1 Answer
I get an error message every time i run this script? Any ideas? 0 Answers
Unable to set position of instantiated prefab 1 Answer
My code is instantiating many prefabs, i only want one. 1 Answer
Generating prefabs at origin that are children of moving GameObjects? 2 Answers