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 /
This question was closed May 01, 2018 at 12:37 PM by connorwforman for the following reason:

I am an idiot

avatar image
0
Question by connorwforman · Apr 30, 2018 at 10:49 PM · scripting problemrigidbodyinstantiate

Projectile not firing

Yay, back to the same problem. This script, it will not fire when mouse0 is clicked.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class AssaultRifle : MonoBehaviour {
 
     public Rigidbody bullet;
     public GameObject gun_end;
     public float bullet_velocity = 10f; 
 
     void Update ()
     {
         
     }
 
     void Shoot ()
     {
         if (Input.GetKeyDown(KeyCode.Mouse0))
         {
             Rigidbody clone = (Rigidbody)Instantiate(bullet, gun_end.transform.position, gun_end.transform.rotation);
             clone.velocity = Vector3.forward * bullet_velocity;
         }
          
     }
 }
 
Comment
Add comment · Show 1
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 ignacevau · Apr 30, 2018 at 10:54 PM 0
Share

You should consider calling your Shoot method.

2 Replies

  • Sort: 
avatar image
1
Best Answer

Answer by shadowpuppet · Apr 30, 2018 at 11:02 PM

Mouse0 should be in the Update function, no?

  void Update(){
     
      if (Input.GetKeyDown(KeyCode.Mouse0))
     Shoot();
     }






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
avatar image
0

Answer by KittenSnipes · May 01, 2018 at 08:57 AM

@connorwforman

Now it should work.

  using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  
  public class AssaultRifle : MonoBehaviour {
  
      public Rigidbody bullet;
      public GameObject gun_end;
      public float bullet_velocity = 10f; 
  
      void Update ()
      {
          Shoot();
      }
  
      void Shoot ()
      {
          if (Input.GetKeyDown(KeyCode.Mouse0))
          {
              Rigidbody clone = (Rigidbody)Instantiate(bullet, gun_end.transform.position, gun_end.transform.rotation);
              clone.velocity = Vector3.forward * bullet_velocity;
          }
           
      }
  }
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 shadowpuppet · May 01, 2018 at 12:44 PM 0
Share

why put Shoot() in the Update function? It's just saying every frame go to Shoot whether you are shooting or not. why not just put the if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.$$anonymous$$ouse0)) in the Update function so it only goes to Shoot if you are shooting

   using System.Collections;
   using System.Collections.Generic;
   using UnityEngine;
   
   public class AssaultRifle : $$anonymous$$onoBehaviour {
   
       public Rigidbody bullet;
       public GameObject gun_end;
       public float bullet_velocity = 10f; 
   
       void Update ()
       {
  if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.$$anonymous$$ouse0))
         
           Shoot();
       }
   
       void Shoot ()
       {
        
               Rigidbody clone = (Rigidbody)Instantiate(bullet, gun_end.transform.position, gun_end.transform.rotation);
               clone.velocity = Vector3.forward * bullet_velocity;
           }
            
       }
   }

Follow this Question

Answers Answers and Comments

176 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 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

OnCollisionEnter not triggering when two rigidbody collide via Instantiate 1 Answer

transform.forward not always forward? 2 Answers

Applying force to a rigidbody 2 Answers

Instantiate rigidbody C# 1 Answer

nullrefernceexception 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