I am trying to get my bullets to shoot the crosshair and it does this instead (the continuous bullets are a feature in my game so I used it to help demonstrate where the bullets go actually go).
Here is the picture:
And here’s my code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerWeapon : MonoBehaviour {
public GameObject bulletPrefab;
public Transform bulletSpawn;
public float bulletSpeed = 30;
public float lifeTime = 3;
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Fire();
}
if (Input.GetKey(KeyCode.Mouse1))
{
Fire();
}
}
private void Fire()
{
GameObject bullet = Instantiate(bulletPrefab);
Physics.IgnoreCollision(bullet.GetComponent<Collider>(), bulletSpawn.parent.GetComponent<Collider>());
bullet.transform.position = bulletSpawn.position;
Vector3 rotation = bullet.transform.rotation.eulerAngles;
bullet.transform.rotation = Quaternion.Euler(rotation.x, transform.eulerAngles.y, rotation.z);
bullet.GetComponent<Rigidbody>().AddForce(bulletSpawn.forward * bulletSpeed, ForceMode.Impulse);
StartCoroutine(DestroyBullerAfterTime(bullet, lifeTime));
}
private IEnumerator DestroyBullerAfterTime(GameObject bullet, float delay) {
yield return new WaitForSeconds(delay);
Destroy(bullet);
}
}
Please help
(I think this person can help)