Making a top down shooter and using raycasting for the gun firing, but want to add physical projectiles to attach some particles to that will fire at the same time as the ray is being cast. Currently I have the rays casted and their cast angle changed with some randomisation, but I can’t figure out how to get the projectiles to travel along the same path as the ray they are created after.
public void Shoot()
{
if (CanShoot())
{
Vector3 shootDirection = (transform.forward);
shootDirection.x += Random.Range(-1f, 1f) * spreadAngle;
shootDirection.z += Random.Range(-1f, 1f) * spreadAngle;
Ray ray = new Ray(spawn.position, (shootDirection.normalized));
RaycastHit hit;
float shotDistance = 20f;
if (Physics.Raycast(ray, out hit, shotDistance))
{
shotDistance = hit.distance;
}
Debug.DrawRay(ray.origin, ray.direction * shotDistance, Color.red, 1f);
nextPossibleShootTime = Time.time + secondsBetweenShots;
Instantiate(bulletProjectile, spawn.position, transform.LookAt(hit.point));
// Instantiate(gameObject as specified in variables, position to instantiate, rotation of instantiated gameObject)
if (tracer)
{
StartCoroutine("RenderTracer",ray.direction * shotDistance);
}
}
}
This is what I have so far, but I can’t figure out how to get the Instantiate function to accept a hit.point, or how to convert the hit point into something usable for the instantiation.