Hi everyone, I made a bullet using linecast in a 2.5D space shooter game, however it has a problem when the collider hits the bullet before the bullet hits the collider, so the linecast ignores the collider from the starting point and finally does not collide.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BulletScript : MonoBehaviour
{
public WeaponScript Owner;
[HideInInspector]
public Vector2 FireSpot;
public string Team;
[HideInInspector]
public Vector2 LastPosition;
public float BulletSpeed;
public float Pierce;
void Start()
{
LastPosition = transform.position;
}
void Update()
{
transform.Translate(FireSpot * BulletSpeed * Time.deltaTime);
if (Physics.Linecast(LastPosition, transform.position, out RaycastHit HitInfo, LayerMask.GetMask("Hittable")))
{
Hit(HitInfo.collider, HitInfo.point);
}
LastPosition = transform.position;
}
// for subsequent hits
public void PostHits(Vector2 Start)
{
if (Physics.Linecast(Start, end: transform.position, out RaycastHit HitInfo, LayerMask.GetMask("Hittable")))
{
Hit(HitInfo.collider, HitInfo.point);
}
}
public virtual void Hit(Collider other, Vector2 Point)
{
if (other.GetComponent<RPGBasicScript>())
{
if (other.GetComponent<RPGBasicScript>().Team != Team)
{
// { damage calculations }
if (Pierce > 0)
{
Pierce--;
PostHits(Point);
}
else
{
Destroy(gameObject);
}
}
}
}
}
I need a faster projectile that the rigidbody supports and that penetrates enemies, I tried to change the order of the scripts but without success, thanks to all.