Hello people,
Somewhere I am doing a mistake on my grenade launcher , and my brain is not responding me to understand where.
Issue: When we activate grenade launcher, it throws grenade to different location.
Explanation: We loot grenades from the map in order to use grenade launcher. if we have grenade in the slot, then we can launch. Player needs to swap the gun to grenade launcher by pressing either G or 5, and then left click on the mouse should instantiate the grenade. We are able to count slot, and we are able to instantiate the grenade, however it does not throwing grenade to the direction we are aiming to.
Source codes:
GrenadeThrower ( Attached to the gun ):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class GrenadeThrower : MonoBehaviour
{
[SerializeField] Camera FPCamera;
[SerializeField] Ammo ammoSlot;
[SerializeField] AmmoType ammoType;
[SerializeField] TextMeshProUGUI grenadeText;
public GameObject grenpref; //yes
[SerializeField] float reloadTime = 2f; //yes
public Transform grenBarrel; //yes
private float lastFireTime;
public float throwforce = 400f;
public void DisplayGrenade()
{
int currentGrenade = ammoSlot.GetCurrentAmmo(ammoType);
grenadeText.text = currentGrenade.ToString();
}
// Update is called once per frame
public void Update()
{
DisplayGrenade();
if (Input.GetMouseButtonDown(0) && Time.time > lastFireTime + reloadTime)
{
if (ammoSlot.GetCurrentAmmo(AmmoType.HPGrenade) > 0)
{
GameObject gren = Instantiate(grenpref, grenBarrel.position, transform.rotation);
//GameObject go = Instantiate(grenpref, grenBarrel.position, Quaternion.LookRotation(grenBarrel.localScale));
Rigidbody rb = gren.GetComponent<Rigidbody>();
rb.AddForce(transform.position * throwforce); //transform.position.normalized
Debug.Log(rb.transform.position + " RB Position");
// Physics.IgnoreCollision(GetComponent<Collider>(), go.GetComponent<Collider>());
lastFireTime = Time.time;
FindObjectOfType<Ammo>().ReduceCurrentAmmo(AmmoType.HPGrenade);
}
}
}
//void ThrowGren()
//{
// GameObject gren = Instantiate(grenpref, transform.localScale, transform.rotation);
// Rigidbody rb = gren.GetComponent<Rigidbody>();
// rb.AddForce(transform.position * throwforce, ForceMode.VelocityChange);
//}
// yield return new WaitForSeconds(reloadTime);
}
Grenade script( referncing to grenpref object on grenadethrower script):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Grenade : MonoBehaviour
{
public float radius = 35f;
public float delayt = 3f;
float cntdwn;
bool hasExploded = false;
public GameObject explosionEff;
public float force = 700f;
[SerializeField] float damage = 100f;
void Start()
{
cntdwn = delayt;
}
void Update()
{
cntdwn -= Time.deltaTime; // each second cntdwn will decrease 1
if (cntdwn <= 0f && hasExploded == false)
{
Explode();
hasExploded = true;
}
}
void Explode()
{
//EXPLOSION
Instantiate(explosionEff, transform.position, transform.rotation);
Collider[] collidersToDestroy = Physics.OverlapSphere(transform.position, radius);
foreach (Collider nearbyObject in collidersToDestroy)
{
Rigidbody rb = nearbyObject.GetComponent<Rigidbody>();
if (rb != null)
{
rb.AddExplosionForce(force, transform.position, radius);
}
}
Destroy(gameObject);
////DEALING DAMAGE
//DEALING DAMAGE TO ZOMBIES
RaycastHit hit;
Collider[] objectsInRange = Physics.OverlapSphere(transform.position, radius);
foreach (Collider col in objectsInRange)
{
EnemyHealth enemy = col.GetComponent<EnemyHealth>();
if (enemy != null)
{
enemy.TakeDamage(damage);
if (Physics.Raycast(transform.position, transform.forward, out hit, radius))
{
EnemyHealth target = hit.transform.GetComponent<EnemyHealth>();
if (target == null) return;
target.TakeDamage(damage);
}
else
{
return;
}
}
//DEALING DAMAGE TO PLAYER
RaycastHit playerhit;
Collider[] playerInRange = Physics.OverlapSphere(transform.position, radius);
foreach (Collider plyr in playerInRange)
{
PlayerHealth player = plyr.GetComponent<PlayerHealth>();
if (player != null)
{
player.TakeDamage(damage);
if (Physics.Raycast(transform.position, transform.forward, out playerhit, radius))
{
PlayerHealth targetMe = playerhit.transform.GetComponent<PlayerHealth>();
if (targetMe == null) return;
targetMe.TakeDamage(damage);
}
else
{
return;
}
}
}
}
}
}
Is there anyone who can help me here please? Thanks in advance