So, this i’m writing a weapon handling system, and this is the logic i want to use
First: in a Weapon List i add some weapons classes, and at the beginning of the game i set the weapon.
By doing so, i get the muzzle position of the weapon, and i guess that the problem is there
With some Debug.Log(); i checked that Unity finds the bulletSpawn, but i don’t know how to solve the problem…
These are the two classes involved in the problem:
//weapon.cs
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
public class Weapons : MonoBehaviour
{
#region private
private GameObject player; //REMINDER: MATT MUST FIX THE PLAYER IN THE GameManager.cs
private Transform bulletSpawn;
private IWeapon ActualWeapon;
List<IWeapon> weapons;
#endregion
#region public
public Transform weaponPositionOnPlayer;
public Rigidbody bullet;
public GameObject[] guns;
public Animation[] reloadAnimations;
#endregion
// Use this for initialization
void Start () {
player = GameObject.Find("Player");
#region Checks
if (guns.Length == 0)
{
Debug.Log("Load some meshs!");
EditorApplication.isPaused = true;
}
/* if (bulletSpawn.Length == 0)
{
Debug.Log("Load some bulletSpawns!");
EditorApplication.isPaused = true;
}*/
if (reloadAnimations.Length == 0)
{
Debug.Log("Load some Animations!");
EditorApplication.isPaused = true;
}
#endregion
weapons = new List<IWeapon>
{
///Insert here all the weeapons needed.
new WeaponsBaseClass(guns[0], bulletSpawn, reloadAnimations[0], 10, 2, 0.5f, 1000f, bullet),
new RocketLauncher(guns[1], bulletSpawn, reloadAnimations[1], 20, 1, 0.3f, 100, bullet)
};
SetWeapon(weapons[1], guns[1]);
}
// Update is called once per frame
void Update () {
// if (Input.GetKey(KeyCode.R)) SetWeapon(weapons[1]);
if (Input.GetMouseButton(0))
{
ActualWeapon.Fire();
}
}
void SetWeapon(IWeapon _weapon, GameObject _gun)
{
ActualWeapon = _weapon;
GameObject tempoGun = Instantiate(_gun, weaponPositionOnPlayer.position, _gun.transform.rotation) as GameObject;
Debug.Log("New bullet spawn transform is: " + tempoGun.transform.GetChild(0).name);
tempoGun.transform.parent = player.transform.GetChild(0);
}
}
and
//RocketLauncher.cs
using UnityEngine;
using System.Collections;
public class RocketLauncher : IWeapon {
private usefulStuff crax = new usefulStuff();
private bool canShoot = true;
public float projectileSpeed;
public float bulletTime;
private float t = 0f;
public Rigidbody bullet;
public int maxBullets, maxBulletsPerCharger;
private int actualBullets;
public Animation reloadAnimation;
public Transform bulletSpawn;
public GameObject weaponMesh;
public RocketLauncher(GameObject wm, Transform bs, Animation ra, int mb, int mbpc, float bt, float ps, Rigidbody b)
{
weaponMesh = wm;
bulletSpawn = bs;
reloadAnimation = ra;
maxBullets = mb;
maxBulletsPerCharger = mbpc;
actualBullets = mbpc;
bulletTime = bt;
projectileSpeed = ps;
bullet = b;
}
// Use this for initialization
public void Fire () {
if (actualBullets > 0)
{
//Check if the player can shoot (REALLY LOL CPTN OBVIOUS STRIKEZ AGAIN )
if (canShoot)
{
//Logic: Remove a bullet, create it then move it. Easy peasy.
actualBullets--;
Rigidbody nBullet = GameObject.Instantiate(bullet, bulletSpawn.position, bulletSpawn.rotation) as Rigidbody; /// Unity says that the error is here
Debug.Log("SHOT!");
//And now the player cant shoot. Cause nobody wants a sniper mitra.
//canShoot = false;
}
/*else
{
//All this stuff is to make a little amount of time pass between two bullets
if (t <= bulletTime)
{
t += Time.deltaTime;
}
else
{
canShoot = true;
t = 0;
}
}*/
}
else
{
Reload();
}
}
// Update is called once per frame
public void Reload () {
canShoot = false;
actualBullets = maxBulletsPerCharger;
//reloadAnimation.Play();
//crax.Delay(reloadAnimation.clip.length);
canShoot = true;
}
}
If someone can help… well, thanks