- Home /
How to shoot with a raycast?
I know that there are millions of tutorials on YouTube and even unity3d.com, but I cannot seem to be able to get the raycast to work. I have tried millions of times to shoot raycasts and even simple mesh spheres. So, so here is the list of questions I need answered:
Which is easier; Launching a mesh sphere or shooting a raycast?
If answer is mesh sphere, go to question 2. If answer is raycast, go to question 3.
Is there a script or a YouTube tutorial to launching a sphere in the direction the character is facing? insert link if so or insert a script
Is there a script or a YouTube tutorial (besides the one made by Unity) to instruct me how to do this? insert link if so or insert a script
Sorry about the whole "Choose Your Own Adventure" style type of question. This is a desperation call for this kind of work, I have not been able to get any other videos or scripts to work.
A Raycast is more efficient to use. Check the Scripting API for all possible syntax possibilities.
Use Debug.DrawRay or Debug.DrawLine to visualize a ray so you know your start and end (or direction) vectors are correct.
Post the code you have tso far so others can try to find out where the error might lie.
Also, please provide more details. Where exactly is the problem? Do you get compiler errors, or does the Raycast not work as intended, ...? Just saying "I cannot seem to be able to get the raycast to work" is very unspecific. Throw a bone and others don't have to guess :)
I agree with @Cherno Please try to get this to work and if you fail, try again, and if you fail again, ask. Provide as much information as possible, when it fails, what happens etc. and add your code so that we can assist with potential problems.
Cheers!
Answer by Trevdevs · Jun 29, 2017 at 05:14 AM
Highly recommend you check out brackeys tutorial for this https://www.youtube.com/watch?v=mpxim8YbsMk
Hes very good at explaining stuff and Is my go to person when I don't understand something other than the unity script API
May be outdated so some stuff might not be correct in that case here is a more recent one done by him https://www.youtube.com/watch?v=THnivyG0Mvo
Answer by omerselman · Jun 29, 2017 at 01:31 PM
This is a very simple raycast shoot script your may use. Add a transform for gunEnd and a float for damage. you can check survival game tutorial for health script and apply damage. Firing audio is basicly an auidoSource and explosion is a particle system prefab. hope it helps. Btw i suggest you to use raycast shooting. It is much efficent and also you can add object pooling with bullettrail objects to visualise bullets. If you dont shoot that much and if the bullet velocity is not much you can use projectile shooting. But in anycase Raycast shooting is way better.
if (CrossPlatformInputManager.GetButtonDown("Shoot")) // Change input this is for crossplatform.
{
RaycastHit hit;
Vector3 rayOrigin = gunEnd.transform.position;
if (Physics.Raycast(rayOrigin, gunEnd.transform.forward, out hit, weaponRange))
{
firingAudio.Play();
if (hit.collider != null)
{
Explosion(hit.point);
ApplyDamage(hit.point);
}
}
}
Your answer
Follow this Question
Related Questions
setting a bool on another object's animator by player's raycast 2 Answers
Why is my raycast projecting from the middle instead of the bottom? 0 Answers
I need help with raycasts and with color of gameobject [SOLVED] 3 Answers
Spawn script on mesh crashes Unity 0 Answers
[C#] Quaternion Rotations with Input.GetAxis problems. 1 Answer