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
0
Question by JoshMBeyer · Mar 31, 2014 at 06:10 PM · c#weaponadvice

Creating a weapon script. Need Help / advice.

I am trying to create a more advance weaponScript. I haven't gotten far at all yet because I am running into a problem "use of unassigned variable" I understand what this means, but I thought it would work be cause the variable "hit" I declared as a RaycastHit. Idk I am doing something wrong though, I know that. So please take a look at the script and tell me what the problems are with it. Also if there is any criticism on how I am going about any part please let me know. I want to do things the cleanest and most efficient way possible.

 using UnityEngine;
 using System.Collections;
 
 
 
 public class weaponScript : MonoBehaviour
 {
     //Clips And Ammo
     public int currentAmmo;
     public int clipCapacity;
     public int numberOfClips;
 
     //Fire Rate(Add burst)
     public float fireRate;
     public float fireTime;
     public float nextFireTime;
 
     //FX
     public  GameObject hitStaticFX;
     public GameObject hitPlayerFX;
     public GameObject bulletHole;
     
     //Functionality.
     public int damage;
 
    
     void Update()
     {
         //If we try to fire and we are able to
         if (Input.GetMouseButton(0) && Time.time >= nextFireTime)
         {
             //Tell us we can fire.
             Debug.Log("Fire Ready");
             
             //Declare a ray, And the ray hitpoint.
             Ray ray = Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
             RaycastHit hit;
 
             //if the hit GO's tag is...
             switch (hit.transform.tag)
             {
                 case "bullet":
                     // do nothing if 2 bullets collide
                     break;
 
                 case "Player":
                     // add blood effect
                     Instantiate(hitPlayerFX, hit.point, Quaternion.FromToRotation(Vector3.forward, hit.normal));
                     break;
 
                 case "Static":
                     // add wood impact effects
                     //Can use this to add a decal where the "projectile" hits.
                     Instantiate(hitStaticFX, hit.point, Quaternion.FromToRotation(Vector3.forward, hit.normal));
                     break;
                 
                 case "ground":
                     // add dirt or ground  impact effect
                     break;
                 
                 default: // default impact effect and bullet hole
                     Instantiate(hitStaticFX, hit.point + 0.1f * hit.normal, Quaternion.FromToRotation(Vector3.up, hit.normal));
                     GameObject newBulletHole = Instantiate(bulletHole, hit.point, Quaternion.FromToRotation(Vector3.up, hit.normal)) as GameObject;
                     newBulletHole.transform.parent = hit.transform;
                     break;
             }
 
             //Add time to next fire.
             nextFireTime = Time.time + fireRate;
         }
     }
 }
 
 
    
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
0
Best Answer

Answer by Leuthil · Mar 31, 2014 at 06:22 PM

You have to actually do a Raycast, which is nowhere in your code :).

This example from the Unity Docs is probably useful to you:

 using UnityEngine;
 using System.Collections;
 
 public class Example : MonoBehaviour {
     void Update() {
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         RaycastHit hit; // Here hit is still not assigned
         if (Physics.Raycast(ray, out hit, 100))
             // The Physics.Raycast() function has now assigned 'hit', so you can use it now!
             Debug.DrawLine(ray.origin, 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

21 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

Related Questions

Multiple Cars not working 1 Answer

Finding the player that walked into a ray? 1 Answer

This is a sloppy conversion from javascript to C#, I cannot seem to get it working... 2 Answers

Selecting and deselecting not working 0 Answers

Edit this code? 0 Answers


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