Shooting with Raycasts - Unity Tutorial
Why Stack Overflow recommends it
Stack Overflow users cite it as the tutorial they followed to build a raycast gun, then link it when asking why their shooting, impact, or auto-fire code behaves unexpectedly.
Recommended by 0 people across 0 different months, 2019 to 2022.
What it is
A Brackeys Unity tutorial that builds a shooting gun using raycasts, including muzzle flash, impact effects, and applying force to rigidbodies that get hit. It is a hands-on, follow-along walkthrough for beginners, narrated by the Brackeys channel host.
What people said
Cites Bigi32 · 2019-02 · Stack Overflow · on “rigid body moves by addForce but the object itself doesn't”
I'm following this video [this video] and This is my shoot function void Shoot() { muzzleFlash.Play(); shootingSound.Play(); RaycastHit hit; if( Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range)) { //Debug.Log(hit.transform.name); Enemy1 target = hit.transform.GetComponent<Enemy1>(); if (target != null) { target.TakeDamage(damage); } if(hit.rigidbody != null) { Debug.Log("******************"); hit.rigidbody.AddForce( - hit.normal * impactForce); } GameObject impactGo = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal)); Destroy(impactGo, 0.3f); } } Rigidbody added to target: [LINK] Barrel component: enter image description here [LINK] This
Cites Jack Godfrey · 2020-02 · Stack Overflow · on “Unity3D - Raycast not hitting correct position”
I'm trying to make a gun by shooting a ray from the centre of the camera which the gun is a child of, I also have a particle effect where the impact happens only, the impact happens in the wrong position. I did Debug.DrawLine and saw that the origin of the ray was happening 0.8 units above the center of the camera, and it sometimes hits directly on the camera Here is the gun script using UnityEngine; public class GunShoot : MonoBehaviour { public float gunDamage = 1f; public float fireRate = .1f; // wait x seconds to fire again public float weaponRange = 100f; public float hitForce = 100f; public Camera playerCam; //public ParticleSystem muzzleFlash; public GameObject impactEffect; private f
Cites Shaurya Varshney · 2022-05 · Stack Overflow · on “Unity - help needed to touch and hold UI button to call update method and auto fire (here is the script)”
I am a newbie with no coding experience just started using unity I was following brackeys Ray cast shooting video, [this video] and I created the gun script but I wanted to turn it into mobile so I wanted to autofire by holding a UI button. plz, I need help me know no code. this is my code. I was thinking is there a way to put something in place of input.getbutton to make a button that will autofire on holding. Thanks & sorry if this is a silly question using System.Collections; using System.Collections.Generic; using UnityEngine; using Unity.VisualScripting; public class Gun : MonoBehaviour { public float damage = 10f; public float range = 100f; public float fireRate = 15f; public float impactFo