Hi, i am developing a fast pace Destruction Game thingy and i got a Problem with the physical Bullet, which i cant figure out.
In the Game there is a Bullet Type called “Heavy Hitter”. It basically launches a heavy ball with good speed to knock stuff over. Thing is, this only happens like 80% of the time. Sometimes, it is launched, stays visible for a split second and then disappears. With the Editor, i found out that it places itself a few to about 100m away when that happens.
The Bullet itself is instantiated. It consists out of 2 parts, the Logical Bullet and the physical Projectile (Screenshots down below). The logical Bullet contains the script itself (Code down below) and then “Pushes” the actual Bullet from its position. The positions itself is calculated by the Player orientation and the Size of the Bullet with a bit of room to spare.
I even rewrote the entire Bullet system to get a fresh start on that. Also, i tried to Increase the distance with speed (It seems to happen more, the faster i go), but even with like 10m distance, it doesn’t help. I am starting to suspect that the Unity Physics engine is somewhat to blame, or maybe i am doing all this wrong…
I even would rewrite the code from scratch with another approach, if somebody told me how to do this
Thanks i advance!
(Player_NewBullet)
using UnityEngine;
public class Player_NewBullet : MonoBehaviour
{
public BulletTypes bulletType;
public int scoreCost;
public bool raycast = true;
public GameObject physicalObject;
public bool explosive = false;
public float cooldown;
public int lifetime = 100;
public float BulletDamage;
public float BulletSpeed;
public float explosionStrength = 1000;
public float explosionRadius = 24;
public GameObject explosionParticles;
public void ShootShot(Player_Movement player_Movement)
{
Vector3 direction = player_Movement.fetchCamRotation().forward;
Vector3 startingPos = player_Movement.playerRB.transform.position;
if (raycast)
{
Ray ray = new Ray(startingPos, direction);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
if (hit.transform.TryGetComponent<Rigidbody>(out Rigidbody rbHit))
{
rbHit.AddForce(direction * BulletDamage);
//print("Hit something");
}
if (hit.transform.TryGetComponent<Shooting_Interaction>(out Shooting_Interaction shotHit))
{
shotHit.Interaction(1);
if (explosive)
{
Vector3 explosionPos = hit.point;
Collider[] colliders = Physics.OverlapSphere(explosionPos, explosionRadius);
Instantiate(explosionParticles, explosionPos, Quaternion.identity);
foreach (Collider expHit in colliders)
{
Rigidbody rb = expHit.GetComponent<Rigidbody>();
if (rb != null)
rb.AddExplosionForce(explosionStrength, explosionPos, explosionRadius, 2.0F);
if (expHit.transform.TryGetComponent<Shooting_Interaction>(out Shooting_Interaction expShotHit))
{
expShotHit.Interaction();
}
}
SoundManager.instance.PlayEffectClip("explosion_rocket", explosionPos);
}
}
print(hit.transform.name + " " + hit.point.ToString());
}
}
else
{
physicalObject.transform.position = gameObject.transform.position + ((direction * (player_Movement.playerRB.GetComponent<SphereCollider>().radius +
physicalObject.GetComponent<SphereCollider>().radius) * 1.2f + new Vector3(0f, 0.1f, 0f)));
physicalObject.GetComponent<Rigidbody>().AddForce(direction * BulletDamage, ForceMode.Acceleration);
}
}
private void FixedUpdate()
{
lifetime--;
if(lifetime < 0)
{
Destroy(gameObject);
}
}
}
(Player_NewShooting)
using UnityEngine;
using UnityEngine.InputSystem;
public class Player_NewShooting : MonoBehaviour
{
[SerializeField]
Player_NewBullet[] bullets;
public InputActionReference shootInteraction;
public InputActionReference changeBullet;
public InputActionReference inputActions_Look;
public Player_Movement pm;
BulletTypes currentBullet;
int currentBulletIndex = 0;
public Player_NewBullet[] allBullet;
private void Awake()
{
currentBulletIndex = allBullet.Length;
ChangeAmmo(new InputAction.CallbackContext());
}
public BulletTypes FetchBulletType()
{
return currentBullet;
}
bool shot = false;
private void FixedUpdate()
{
Vector2 lookVector = inputActions_Look.action.ReadValue<Vector2>();
if(lookVector.y > 0.5 & shot == false)
{
shot = true;
Shoot(new InputAction.CallbackContext());
}
if(lookVector.y < 0.4)
{
shot=false;
}
}
private void OnDrawGizmosSelected()
{
Vector3 direction = pm.fetchCamRotation().forward;
Vector3 startingPos = pm.playerRB.transform.position;
Gizmos.DrawRay(startingPos, direction * 100f);
Gizmos.DrawSphere(startingPos + direction, 0.3f);
}
public void ChangeAmmo(InputAction.CallbackContext obj)
{
if(currentBulletIndex + 1 >= allBullet.Length)
{
currentBulletIndex = 0;
}
else
{
currentBulletIndex++;
}
currentBullet = allBullet[currentBulletIndex].bulletType;
//print(allBullet.Length);
}
public void Shoot(InputAction.CallbackContext obj)
{
Player_NewBullet thisBullet = allBullet[0];
foreach (Player_NewBullet bullet in allBullet)
{
if(bullet.bulletType == currentBullet)
{
thisBullet = bullet;
break;
}
}
if (Score_Master.instance.RemoveScore(thisBullet.scoreCost))
{
Player_NewBullet bullet = Instantiate(thisBullet);
bullet.transform.position = pm.playerRB.transform.position;
bullet.transform.rotation = pm.transform.rotation;
bullet.ShootShot(pm);
}
}
public void EnableShooting()
{
shootInteraction.action.started += Shoot;
changeBullet.action.started += ChangeAmmo;
}
public void DisableShooting()
{
shootInteraction.action.started -= Shoot;
changeBullet.action.started -= ChangeAmmo;
}
void SpawnBullet()
{
}
}