Skip to content

Turret Quaternion Look At and Instantiate Projectile

public class TurretQuaternionSystemLookAt : MonoBehaviour
{
public Transform target;
public float updateDelay = .75f;
public TurretInstantiateProjectile turretInstantiateProjectileScript;

void OnEnable()

{

InvokeRepeating(“StartLookAt”, 0f, updateDelay);

}

void StartLookAt()

{

Vector3 relativePos = target.position – transform.position; transform.rotation = Quaternion.LookRotation(relativePos); turretInstantiateProjectileScript.FireTurretGun();

}

}

public class TurretInstantiateProjectile : MonoBehaviour
{
public Transform turretGunBarrel;

public float bulletRange = 100f;

public float propulsionForce;

public Rigidbody turretBulletPrefab;

public float nextActionTime = 2.0f;

public float period = 3.0f;

public void FireTurretGun()

{

Rigidbody turretBulletInstance;

turretBulletInstance = Instantiate(turretBulletPrefab, turretGunBarrel.position, turretGunBarrel.rotation) as Rigidbody;

turretBulletInstance.GetComponent<Rigidbody>().AddForce(turretGunBarrel.forward * propulsionForce, ForceMode.Impulse);

}

}

Leave a Reply

Your email address will not be published.