- Home /
How to get bullets to hit crosshair
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)
Answer by Llama_w_2Ls · May 19, 2021 at 08:46 AM
This code would work if your gun was facing the crosshair at all times. To do this, simply fire a raycast from the camera to the nearest wall, get the hit point, and rotate the gun to face that point. For example:
public Transform Gun;
public Transform cam;
void Update()
{
RotateGun();
}
void RotateGun()
{
if (Physics.Raycast(cam.position, cam.forward, out RaycastHit hitInfo))
{
Vector3 direction = hitInfo.point - Gun.position;
Gun.rotation = Quaternion.LookRotation(direction);
}
}
Thanks this helped a lot but do you know how to stop the bullets from going through walls?
Try the suggestion below. For bullet projectiles, just use hitscan and create a fake bullet effect either with a particle system or line renderer etc.
For larger projectiles, such as rocket launchers or grenades (or if you also want bullets with colliders), you can do a sweep test (as suggested below), and before you move the projectile forward, raycast a certain distance forward, and if it hits something, teleport it to the hit pos, as to ensure that it doesn't move through walls (if it is really fast).
Answer by tuinal · May 21, 2021 at 01:12 AM
You don't typically implement bullets as physics collider objects since any physics system has issues with high velocity. Basically the engine sees the bullet on one side, then at the next physics step on the far side, and there's no frame with a collision. You can tweak rigidbody properties to continuous detection to 'sweep' ahead at each step and try to anticipate a collision, but typically for a fast projectile you raycast an instantaneous hit and create an illusion of a tracer as a visual effect, and/or delay or adjust the impact position for bullet drop etc.
Your answer
Follow this Question
Related Questions
Raycasr in my fps? 1 Answer
How can I shoot bullets that you can aim with the crosshair? 1 Answer
Problem with gun/shooting scripting 0 Answers
raycast spawn problem 0 Answers
How can I make a Grapple Gun work with the Character Controller? 1 Answer