Creating a turrent that uses projectiles

marcrasmussen

New member
I have attempted to create a static turrent that fires a projectile:

Code:
    [SerializeField] public GameObject m_projectile;
    public Transform firePoint;
    private GameObject m_SpawnedProjectile;

    private void OnEnable()
    {
        var rotation = Quaternion.LookRotation(transform.forward);

        m_SpawnedProjectile =
            ObjectPoolBase.Instantiate(m_projectile, firePoint.position, rotation * m_projectile.transform.rotation);
        var projectile = m_SpawnedProjectile.GetCachedComponent<Projectile>();
        projectile.Initialize(Vector3.forward, Vector3.forward * 200, gameObject);
        m_SpawnedProjectile = null;
    }

    private void Update()
    {
        if (Input.GetKeyUp(KeyCode.X))
        {
            var rotation = Quaternion.LookRotation(transform.forward);

            m_SpawnedProjectile = ObjectPoolBase.Instantiate(m_projectile, firePoint.position,
                rotation * m_projectile.transform.rotation);
            var projectile = m_SpawnedProjectile.GetCachedComponent<Projectile>();
            projectile.Initialize(Vector3.forward, Vector3.forward * 200, gameObject);
        }
    }

When this code is executed on enable (the first time) a projectile is fired however when running the update code the projectile just stands still and doesnt move.

Can anyone see what ive done wrong?
 
Have you compared your turret script to the demo scene turret? I would need to insert breakpoints to see what is going on with yours but the demo turret should be a good example.
 
Top