Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by icelated · Aug 29, 2012 at 11:06 PM · c#raycastshooting

Raycast shooting for multiple weapons

I need some advice on how to set-up ray points so i can shoot multiple weapons.

I have 3 + guns. The guns are attached to the FPS capsule in there normal holding position(hip). I have made a simple C# script for shooting. I created an empty cube and placed it infront of one of my guns. In the C# script i used debug and color so i can see the raycast line.

In order for the line to work or should i say "visible" i dont know. Either way, when i hit the f key i had to make the empty cube called "ray point" a child of main camera. Now, this will only shoot from one position.

I only have one camera in my project. From my main camera i have an child empty object named "player weapons" from there its child are my weapons. I dont have a weapons camera.

I am assuming that i need to set up 2 ray cast points(hip and aim) to shoot from?

I have a gun animation that sets it into aiming position.

Im just unsure how i can set this up for each gun and have it follow / shoot wherever its aiming(hip, aim).

 public class RayShootAK47 : MonoBehaviour {
 
     
     public float range = 1000;
     public float force = 1000;
     public RaycastHit hit;
     
     void Start () {
     
     }
     
     
     void Update () {
         
           if(Input.GetButtonDown("Fire1")){
         Rayshoot();
         }
     
     }
     
     
     public  void Rayshoot(){
         
         
         Vector3 direction_ray = transform.TransformDirection(Vector3.forward);
         Debug.DrawRay(transform.position, direction_ray * range, Color.blue);
         if(Physics.Raycast(transform.position, direction_ray, out hit, range)){
             
         }
         
     }
     
 }
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by aldonaletto · Aug 30, 2012 at 12:28 AM

If you want to always hit the center of the screen (where you can draw a crosshair), create a ray based on the camera:

  Ray ray = Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
  if (Physics.Raycast(ray, out hit, range)){
    // something was hit
  }
This would not work for instantiated projectiles because you could see them starting at the camera position, but works fine for raycast shooting, since you can't see the ray anyway.
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 icelated · Aug 30, 2012 at 12:52 AM 0
Share

I think this could be a good solution for me. thank you.

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

8 People are following this question.

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

Related Questions

shooting multiple enemies using raycast 2 Answers

Enemy Shooting Through Raycasts 0 Answers

AI Shooting for Aircraft 1 Answer

Raycast from an objects co-ordinates 1 Answer

Limited ammo from Raycast? 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