How to instantiate weapon shots based off Weapons class abstract implementation?
So I've been trying to make my player shoot my different weapons based on the weapon the user chooses. I've implemented an abstract Weapon class that my classes Rockets,Bullets, and Knifes inherit from. Below is the code i have come up with:
abstract public class Weapons : MonoBehaviour
{
abstract protected void Shoot ();
public GameObject weaponShot;
public float speed;
public float fireRate;
public float nextFire;
public Transform shotSpawn;
public float hitDamage;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
using UnityEngine;
using System.Collections;
public class Bullets : Weapons {
public Rigidbody bullet;
// Use this for initialization
void Start ()
{
}
protected override void Shoot ()
{
if (Input.GetKey(KeyCode.Space) && Time.time> nextFire)
{
nextFire =Time.time + fireRate;
Rigidbody bulletClone = (Rigidbody) Instantiate (bullet,shotSpawn.position,shotSpawn.rotation);
bulletClone.velocity = transform.forward * fireRate;
}
}
// Update is called once per frame
void Update ()
{
Shoot ();
}
}
public class Rockets : Weapons
{
// Use this for initialization
void Start () {
}
protected override void Shoot ()
{
if (Input.GetKey(KeyCode.Space)&& Time.time> nextFire)
{
nextFire =Time.time + fireRate;
GameObject rocket= Instantiate (weaponShot,shotSpawn.position,shotSpawn.rotation)as GameObject;
}
}
void Update()
{
Shoot ();
}
using UnityEngine;
using System.Collections;
public class Knife : Weapons {
public Rigidbody rock;
// Use this for initialization
void Start () {
}
protected override void Shoot ()
{
if (Input.GetKey(KeyCode.Space) && Time.time> nextFire)
{
nextFire =Time.time + fireRate;
Rigidbody rockClone= (Rigidbody) Instantiate (rock,shotSpawn.position,shotSpawn.rotation);
rockClone.velocity = transform.forward * fireRate;
}
}
// Update is called once per frame
void Update ()
{
Shoot ();
}
}
Can someone please help me?
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612101502im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Unity 2D - Programming enemies for a 2D top down car game (c#) 0 Answers
The location of the object by points 0 Answers
How to add a " Achievement unlocked " text and a collectible after completing all objectives 0 Answers
How to interrupt wait coroutine in a loop? 0 Answers
Let the Camera Face the middle of the Map while still following the Player? 1 Answer