Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by connorwforman · Jun 29, 2017 at 12:11 AM · scripting problemraycastscripting beginner

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:

  1. 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.

  1. 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

  2. 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.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Cherno · Jun 29, 2017 at 12:46 AM 1
Share

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 :)

avatar image yoHasse · Jun 29, 2017 at 08:14 AM 0
Share

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!

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image connorwforman · Jun 29, 2017 at 12:26 PM 0
Share

Thank you so much!

avatar image
0

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);
                     }
 
             }
 
         }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

132 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges