Question by
sertanbalci3440 · May 22, 2021 at 11:38 AM ·
raycastphysics2draycasthit2dphysics2d.raycast
I think my raycast detects destroyed gameobjects
So im new to unity and learning it while developing my first game. In the game your shooting ability is synchronized with music like rythm games and i use raycast system for shooting. so my problem is sometimes my "laser" stops at where the last enemy died which im not wanting. And sometimes the laser does not coming from the players mouth that is a part of the problem too. If you watch the video you can see the exact problem. Here is the video
Here is the code for raycast shooting system:
public IEnumerator Shoot()
{
RaycastHit2D hitInfo = Physics2D.Raycast(rcPoint.position, rcPoint.up);
if (hitInfo)
{
Enemy enemy = hitInfo.transform.GetComponent<Enemy>();
if (enemy != null)
{
lineRenderer.SetPosition(0, rcPoint.position);
lineRenderer.SetPosition(1, hitInfo.point);
Instantiate(impactPrefab, hitInfo.point, Quaternion.identity);
enemy.Die();
}
Boss boss = hitInfo.transform.GetComponent<Boss>();
if (boss != null)
{
lineRenderer.SetPosition(0, rcPoint.position);
lineRenderer.SetPosition(1, hitInfo.point);
boss.Damage(1);
Instantiate(impactPrefab, hitInfo.point, Quaternion.identity);
}
Bullet bullet = hitInfo.transform.GetComponent<Bullet>();
if (bullet != null)
{
lineRenderer.SetPosition(0, rcPoint.position);
lineRenderer.SetPosition(1, hitInfo.point);
Instantiate(impactPrefab, hitInfo.point, Quaternion.identity);
bullet.DestroyBullet();
}
SwordFish swordfish = hitInfo.transform.GetComponent<SwordFish>();
if (swordfish != null)
{
lineRenderer.SetPosition(0, rcPoint.position);
lineRenderer.SetPosition(1, hitInfo.point);
Instantiate(impactPrefab, hitInfo.point, Quaternion.identity);
swordfish.sFishDamage(1);
}
}
else
{
lineRenderer.SetPosition(0, rcPoint.position);
lineRenderer.SetPosition(1, rcPoint.position + rcPoint.up * 20);
}
lineRenderer.enabled = true;
yield return new WaitForSeconds(0.03f);
lineRenderer.enabled = false;
}
if you think the problem is not the raycast detecting destroyed gameobjects, please let me know what you think the problem is
Comment