Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Kury05 · Jul 03, 2014 at 03:54 PM · raycastshooting

Raycast Shooting problem

Hello:) I have a problem with my raycast shooting script, it doesn't shoot:/ This is my script:

 var bulletTex : GameObject[]; 
 var TheDammage = 100;
 var FireRate : float = 0.1;
 var gunFired = false;
 private var Canfire = true;
 var shot : AudioClip;
 var currentBullets = 20;
 private var nextFire : float = 0.0;
 var fireRate : float = 1.0;
 var clips = 5.0;                     
 var ammoCount = 20;
 var ammoInMagazine = 20;
 var reloadTime = 1.5;                                                                                                
 private var t : Transform;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
                                                 
 function Start () {
 
 }
  
 function Update () {
  
 var fwd = transform.TransformDirection(Vector3.forward); 
 
 var hit : RaycastHit;
   
 Debug.DrawRay(transform.position, fwd * 10000, Color.green); 
 
 if(Input.GetButton ("Fire1") && IsOkToShoot() && Physics.Raycast(transform.position, fwd, hit, 10000)){ 
 Shoot();
             
 
 
 }
 }
 
 function IsOkToShoot () : boolean {
     var itsOk : boolean = false;
  
     if (Time.time>nextFire) {
         nextFire = Time.time + fireRate;
         itsOk = true;
     }
  
     return itsOk;
 }
 
 
 function Shoot() {
 
  
 var hit : RaycastHit;
 
 if (currentBullets > 0) {
 
 Instantiate(bulletTex[Random.Range(0,3)], hit.point, Quaternion.FromToRotation(Vector3.up, hit.normal));
 
 AudioSource.PlayClipAtPoint(shot, t.position);
     currentBullets--; //subtract one
 }
   if (currentBullets <= 0){
   audio.Play();
    Reload();
 
 }
 
 
 }
 
 function Reload() {
     
     // Wait for reload time first - then add more bullets!
     yield WaitForSeconds(reloadTime);
     
     
     if (clips > 0) {
         clips--;
         
         currentBullets = ammoInMagazine;
     }
 }
 


Thank you for any response, sorry for my bad English, I'm from Czech republic :)

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Lazdude17 · Jul 03, 2014 at 05:47 PM

Which raycast isn't working? One thing to note is that TransformDirection.forward will not always be what you want it to be because to truly be forward all the time it has to be local coordinates. So if your gun or whatever you are actually shooting from has local coordinates that are different than expected then the raycast will just shoot wherever the +Z axis points. So just tick that local/global button at the top of unity to switch coordinate views and make sure that the Z is pointing where you want to shoot.

Comment
Add comment · Show 2 · 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 Kury05 · Jul 03, 2014 at 08:47 PM 0
Share

"Z" is always pointing forward:)

avatar image Lazdude17 · Jul 04, 2014 at 06:02 PM 0
Share

Is it doing anything? Have you tried going through and debugging with print statements?

avatar image
0

Answer by Juice-Tin · Jul 04, 2014 at 07:12 PM

Hi! I have some helpful raycast debugging functions you can try. Put these in your class, and then use ShowRaycast in your 'if' statement instead of Physics.Raycast. It works the same but will always display a red line of the raycast if nothing is hit, and a green line if something is hit. The line is drawn perfectly on the actual raycast, so you should be able to find your problem a lot easier. :)

(You can remove your Debug.drawRay, as this will auto draw it for you)

 public void DrawRay2(Vector3 start, Vector3 dir, float dist, Color color){
         Debug.DrawLine(start, start + dir.normalized * dist, color);
     }
     public bool ShowRaycast(Vector3 start, Vector3 dir, float dist){
         bool raycast = Physics.Raycast(start, dir, dist);
         DrawRay2(start, dir, dist, raycast?Color.green:Color.red);
         return raycast;
     }
     public bool ShowRaycast(Vector3 start, Vector3 dir, out RaycastHit raycastHit, float dist){
         bool raycast = Physics.Raycast(start, dir, out raycastHit, dist);
         DrawRay2(start, dir, dist, raycast?Color.green:Color.red);
         return raycast;
     }
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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Raycast damage script 2 Answers

change to raycast shooting instead of rigidbody shooting 1 Answer

Raycast Shooting Help 1 Answer

NEED HELP WITH RAYCAST SHOOTING !!! 1 Answer

need help converting gun script from c# to javascript 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