Search Unity

projectiles are instantiating at the wrong position

Discussion in 'Prefabs' started by alexandersueme, Jun 23, 2020.

  1. alexandersueme

    alexandersueme

    Joined:
    Jun 3, 2019
    Posts:
    1
    this is a bit of a long one: I've written a script for all magic spells to inherit from in my game that looks like this:

    public bool triggerdown = false;
    public float charge;
    public GameObject shot = null;

    void FixedUpdate()
    {
    if (triggerdown)
    {
    charging();
    }
    }
    public virtual void charging()
    {
    charge += Time.deltaTime;
    }
    public virtual void triggerpress()
    {
    triggerdown = true;
    }
    public virtual void triggerup()
    {
    triggerdown = false;
    }
    }

    and the actual spell I'm trying to use is:

    public class fireballspell : Spell
    {
    public override void charging()
    {
    base.charging();
    Debug.Log("charging");
    }
    public override void triggerpress()
    {
    base.triggerpress();
    }
    public override void triggerup()
    {
    base.triggerup();
    GameObject bullet = Instantiate(shot, gun);
    }
    }

    and it gets called from the players's controller:

    public class playercont : MonoBehaviour
    {
    public Spell Rweapon;
    public Spell weapon;
    public Transform gun1;

    void Start()
    {
    setweaponr();
    }
    private void FixedUpdate()
    {
    if (Input.GetKeyDown(KeyCode.Keypad5))
    {
    Rweapon.triggerpress();
    }
    if (Input.GetKeyUp(KeyCode.Keypad5))
    {
    Rweapon.triggerup();
    }
    }

    public void setweaponr()
    {
    Rweapon = weapon;
    Instantiate(Rweapon, gun1.position, gun1.rotation, gun1);
    }
    }

    everything works absolutely perfect except one thing: when I hit the fire button, it does create a fireball that behaves as it should, but doesn't come out of my hand where the new instance of the Fireballspell is, it comes out of the point in empty space where I originally made the prefab for it. The second I start the game, it is supposed to set the Fireballspell (weapon) to the current active weapon (Rweapon), which it does. A little orange orb is supposed to appear in my hand, which it does, marking the weapon as set. However, when I hit fire, the projectile comes out of empty space.