- Home /
Question by
KashewSucksAtCoding · Jul 23, 2020 at 04:10 AM ·
c#fpsshootingraycasting
How do I make bullet tracers show when I shoot in the air?
I have a bullet tracer script, however when I shoot in the air it doesn't work. I was wondering how I could make the bullet tracer script function when shot in the air as well.
Here is my code:
void Shoot()
{
audioSource.Play();
muzzleflash.Play();
recoil.Fire();
RaycastHit hit;
if (Physics.Raycast(shootPoint.position, shootPoint.transform.forward, out hit, range))
{
UnityEngine.Debug.Log(hit.transform.name);
player2Health target = hit.transform.GetComponent<player2Health>();
SpawnBulletTrail(hit.point);
if (target != null)
{
target.TakeDamage(damage);
}
}
}
void SpawnBulletTrail(Vector3 hitPoint)
{
GameObject bulletTrailEffect = Instantiate(bulletTrail.gameObject, shootPoint.position, Quaternion.identity);
LineRenderer lineR = bulletTrailEffect.GetComponent<LineRenderer>();
lineR.SetPosition(0, shootPoint.position);
lineR.SetPosition(1, hitPoint);
Destroy(bulletTrailEffect, 3f);
}
}
Comment
Answer by MHernandezOlmo · Jul 23, 2020 at 08:08 AM
The best you can do here is to make a fake point target at X distance and spawn the trail with that. For example on the "else" part of your raycast:
if (Physics.Raycast(shootPoint.position, shootPoint.transform.forward, out hit, range))
{
...
}
else{
Vector3 fakeTargetPosition = shootPoint.position +shootPoint.transform.forward*100;
SpawnBulletTrail(fakeTargetPosition);
}
I used 100 but you can use your variable range for example.
Your answer
Follow this Question
Related Questions
Gun Shooting Animation Won't Play 0 Answers
Reload Ammo is not working 0 Answers
Reload Ammo is not working 1 Answer
Multiple Cars not working 1 Answer
shooting multiple enemies using raycast 2 Answers