What my problem is I made a bullet shooting system and I can’t figure out how to make the bullet go in one direction instead of going straight down, Can you please Help?
This is my code
using UnityEngine;
public class Gun : MonoBehaviour
{
public float damage = 10f;
public float range = 100f;
public float fireRate = 15f;
public float impactForce = 30f;
public Camera MainCamera;
public ParticleSystem GunShot;
public GameObject impactEffect;
private float nextTimeToFire = 0f;
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0) && Time.time >= nextTimeToFire)
{
nextTimeToFire = Time.time + 1f / fireRate;
Shoot();
}
}
void Shoot()
{
GunShot.Play();
RaycastHit hit;
if (Physics.Raycast(MainCamera.transform.position, MainCamera.transform.forward, out hit, range))
{
Debug.Log(hit.transform.name);
Target target = hit.transform.GetComponent();
if (target != null) ;
{
target.TakeDamage(damage);
}
if (hit.rigidbody != null)
{
hit.rigidbody.AddForce(-hit.normal * impactForce);
}
GameObject impactGO = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
Destroy(impactGO, 2f);
}
}
}
This is my shooting code
using UnityEngine;
public class Shooting : MonoBehaviour
{
public GameObject bullet;
public float speed = 100f;
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
GameObject instBullet = Instantiate(bullet,transform.position,Quaternion.identity) as GameObject;
Rigidbody instBulletRigidbody = instBullet.GetComponent();
instBulletRigidbody.AddForce(Vector3.forward * speed);
}
}
}