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 rOBY GAMES · May 11, 2015 at 12:48 PM · playerwalkshot

Shot is activated only when I walk.

Hello everyone.

I'm trying to understand the problem in this script. When the character walks the shot works but when the character is still the shot is not working.

This is the script:

 #pragma strict
  
 var Prefab : Rigidbody2D;
 var PrefabSpeed : float = 8;
 
 function Update () 
 {
  
  if(Input.GetButtonDown("Fire1")) 
 {
   
    if(Collisions.GRENADE_AMMO <= 0) 
  
          
         if(!Prefab || !PrefabSpeed)
     
      
   else
   
    {
         if (Input.GetKey(KeyCode.D))
        { 
         var PrefabCreate = Instantiate(Prefab,GameObject.Find("Projectile Spawn").transform.position,Quaternion.identity);
         PrefabCreate.rigidbody2D.velocity = transform.TransformDirection(Vector2.right * PrefabSpeed); 
        } 
         if (Input.GetKey(KeyCode.A))
         {
         PrefabCreate = Instantiate(Prefab,GameObject.Find("Projectile Spawn").transform.position,Quaternion.identity);
         PrefabCreate.rigidbody2D.velocity = transform.TransformDirection(-Vector2.right * PrefabSpeed); 
       }
      }
Comment
Add comment · Show 4
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 Basen · May 11, 2015 at 01:23 PM 0
Share

You are instantiating shots only when either A or D is pressed... Do the same code under fire button.

avatar image rOBY GAMES · May 11, 2015 at 01:28 PM 0
Share

Thanks for the reply.

You mean like this?

 function Update () 
 {
  
  if(Input.GetButtonDown("Fire1")) 
 {
  if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.D)) 
         
 if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.A)) 
        }
avatar image Basen · May 11, 2015 at 02:05 PM 0
Share
 Update()
 {
 if(Input.GetButtonDown("Fire1")
 {
 fire function Instantiate
 }
 
 if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.D)
 {
 move script right
 }
 else if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.A)
 {
 move script left
 }
 }
avatar image Basen · May 11, 2015 at 02:06 PM 0
Share

Basically what your first code was is that IF you were pressing the mouse button AND pressing the D or A key it would fire.

This way it will fire even without D or A

2 Replies

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

Answer by barbe63 · May 11, 2015 at 01:41 PM

You have many if without brackets or action assigned on it and you seems to instatiate your bullets in movement key conditions.

It should looks like this:

 if(Input.GetButtonDown("Fire1")) 
 {
    //fire action
 }
   if (Input.GetKey(KeyCode.D)) 
 {
    //move right action
 }
          
  if (Input.GetKey(KeyCode.A)) 
 {
     //move left action
 }    
Comment
Add comment · Show 6 · 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 rOBY GAMES · May 11, 2015 at 01:58 PM 0
Share

Thanks for the reply.

But this is not at work. Now shoot 10 shots all together without me I hit the fire button.

avatar image barbe63 · May 11, 2015 at 03:48 PM 0
Share

post the new code

avatar image rOBY GAMES · May 11, 2015 at 03:54 PM 0
Share

Thank you. I tried to remove the two inputs. D and A. Now Shoot even when stationary. But shows another problem. Shoot simultaneously both right and left, two shots in one.

 #pragma strict
  
 var Prefab : Rigidbody2D;
 var PrefabSpeed : float = 8;
 //var PrefabCreate = Vector2.right;
 
 static var myObject : GameObject; 
 
 
 
 function Update () 
 {
  
 
 
  if(Input.GetButtonDown("Fire1")) 
 {
   
    
    if(Collisions.GRENADE_A$$anonymous$$$$anonymous$$O <= 0) 
   {
       myObject.SetActive(false);
   } 
          
         if(!Prefab || !PrefabSpeed)
     
       {
             Debug.Log("Il prefab o la velocità non esiste");
       }
       
   else
   
    {
         //if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.D))
        { 
         var PrefabCreate = Instantiate(Prefab,GameObject.Find("Projectile Spawn").transform.position,Quaternion.identity);
         PrefabCreate.rigidbody2D.velocity = transform.TransformDirection(Vector2.right * PrefabSpeed); 
        //} 
         //if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.A))
         //{
         PrefabCreate = Instantiate(Prefab,GameObject.Find("Projectile Spawn").transform.position,Quaternion.identity);
         PrefabCreate.rigidbody2D.velocity = transform.TransformDirection(-Vector2.right * PrefabSpeed); 
       }
      
      
      
      Collisions.GRENADE_A$$anonymous$$$$anonymous$$O --;
      GameObject.Find("g_Count").guiText.text = ""+Collisions.GRENADE_A$$anonymous$$$$anonymous$$O;
      print("YOU NOW HAVE " + Collisions.GRENADE_A$$anonymous$$$$anonymous$$O + " GRENADES");
      }
    }
 }
avatar image barbe63 · May 11, 2015 at 04:02 PM 0
Share

Answer fully edited after reading again:

I had read your code again and what it does is you put all your code into the same main condition if(fire) Then you instatiate twice and add force to the object. First on the right and then on the left. As you said it was doing. Once again read what you do and try to understand it.

Apart from that i don't see why it would fire without you touching the button.

avatar image rOBY GAMES · May 11, 2015 at 04:09 PM 0
Share

Sorry for my mistakes.

I know I have to get along. I appreciate your help. Thank you.

Show more comments
avatar image
0

Answer by rOBY GAMES · May 11, 2015 at 04:34 PM

Hello barbe63.

The problem now that fires 10 rounds together when it enters boxcollider2D. 10 shots fired all together, both right and left.

I show you the code:

 #pragma strict
  
 var Prefab : Rigidbody2D;
 var PrefabSpeed : float = 8;
 //var PrefabCreate = Vector2.right;
 
 static var myObject : GameObject; 
 
 
 
 function Update () 
 {
  
 
 
  if(Input.GetButtonDown("Fire1")) 
 {
    }
    if (Input.GetKey(KeyCode.D)) 
  {
     //move right action
  }
           
   if (Input.GetKey(KeyCode.A)) 
  {
      //move left action
  }    
    
    if(Collisions.GRENADE_AMMO <= 0) 
   {
       myObject.SetActive(false);
   } 
          
         if(!Prefab || !PrefabSpeed)
     
       {
             Debug.Log("Il prefab o la velocità non esiste");
       }
       
   else
   
    {
        
          //if (Input.GetKey(KeyCode.D))
         
         var PrefabCreate = Instantiate(Prefab,GameObject.Find("Projectile Spawn").transform.position,Quaternion.identity);
         PrefabCreate.rigidbody2D.velocity = transform.TransformDirection(Vector2.right * PrefabSpeed); 
         
         //if (Input.GetKey(KeyCode.A))
         
         PrefabCreate = Instantiate(Prefab,GameObject.Find("Projectile Spawn").transform.position,Quaternion.identity);
         PrefabCreate.rigidbody2D.velocity = transform.TransformDirection(-Vector2.right * PrefabSpeed); 
       
      
      
      
      Collisions.GRENADE_AMMO --;
      GameObject.Find("g_Count").guiText.text = ""+Collisions.GRENADE_AMMO;
      print("YOU NOW HAVE " + Collisions.GRENADE_AMMO + " GRENADES");
      }
    }
 //}
Comment
Add comment · Show 7 · 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 barbe63 · May 11, 2015 at 04:52 PM 0
Share

HAHAHA!!!

Dude sorry about that laughing but this is quite fun actually.

I want you to understand what you're doing but I'll give you some clues.

First you ask: is fire key is pressed? then if it is you do whatever are inside the brackets which in your code is NOTHING. Then you do the same for D and A nothing to be done if the condition is filled apart those comment i written to you to explain you what you should put into those brackets (not in detail).

Then you do some check on ammo saying something like if ammo is negative set an object inactive.

And then you're doing a prefab check and if both exist you are firing (that's what the else do, it is literraly ATTACHED to the last if) So?

You are sending the firing instantiation permanently as long as both your prefabs are not null. (and twice, one in the left and one in the right)

I bet that's not what you want!

Again try to understand the logic and read what you do.

Now try to tell me: 1) what is the firing action here? 2) where to put it?

avatar image rOBY GAMES · May 11, 2015 at 05:06 PM 0
Share

Then.

1) Action shot:

if (Input.GetButtonDown ("Fire1"))

PrefabCreate.rigidbody2D.velocity = transform.TransformDirection (Vector2.right PrefabSpeed *);

2) Where to put it?

Now I do not know how to place the shot firing properly. Itself the shooting action shown above is correct, I do not know how to position it. I hope it is correct.

avatar image barbe63 · May 11, 2015 at 09:37 PM 0
Share

the correct answer was

the action is what you want to do if the button fire is hit so it's :

      var PrefabCreate = Instantiate(Prefab,GameObject.Find("Projectile Spawn").transform.position,Quaternion.identity);
              PrefabCreate.rigidbody2D.velocity = transform.TransformDirection(Vector2.right * PrefabSpeed); 
     
     // you may change the Vector2 direction if it's not the direction that you want. I guess it's a platform 2d game so Vector2.right should be nice if the player is facing the right direction
 

where to put it is inside those brackets:

  if (Input.GetButtonDown ("Fire1"))
         {
         //insert the action here
         }
 

then the next trick you have to do if i'm guessing right and you're doing a 2d platofrm game is something like this:

   if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.D))
     {
     //then you know your player is facing right so set a boolean defined before like this
     facingRight=true;
     }
     
     if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.A))
     {
     //this time he's facing left so do this:
     facingRight=false;
     }

And the total code for this is now:

  bool facingRight=true; // set this outside of update but inside the class
          
          void Update()
          {
              if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.D))
              {
              //then you know your player is facing right so set a    boolean defined before like this
              facingRight=true;
              }
           
              if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.A))
              {
              //this time he's facing left so do this:
              facingRight=false;
              }
          
              if (Input.GetButtonDown ("Fire1")) 
              {
                  if(facingRight) //if you are facing right shot to the right
                  {
                      var PrefabCreate = Instantiate(Prefab,GameObject.Find("Projectile Spawn").transform.position,Quaternion.identity);
                      PrefabCreate.rigidbody2D.velocity = transform.TransformDirection(Vector2.right * PrefabSpeed); 
                  }
                  else //else you are facing left, then shoot to the left
                  {
                      var PrefabCreate = Instantiate(Prefab,GameObject.Find("Projectile Spawn").transform.position,Quaternion.identity);
                      PrefabCreate.rigidbody2D.velocity = transform.TransformDirection(-Vector2.right * PrefabSpeed); 
                  }
              }

   

This code is not including the movement but only the facing direction and the fire instantiation. And the rotation of your bullet wouldn't be good also as you have to set it different if you're facing right or left. (Quaternion.identity is to change)

avatar image barbe63 · May 11, 2015 at 09:47 PM 0
Share

I won't make the code for you, the purpose here is for you to understand what is in there. Then you could advance on your own.

avatar image rOBY GAMES · May 12, 2015 at 04:04 PM 0
Share

Hello thank you dui heart for giving me a hand in this script.

$$anonymous$$aybe you're confused but you wrote the script in C #

Look here: bool facingRight = true; and void Update () {}

Show more comments

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

Player shoot up in the air HELP 1 Answer

GameObject not deactivated. 2 Answers

I cant kill the other player. 1 Answer

Network View mess up colliders? 0 Answers

collider then change rotation converse? 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