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 /
  • Help Room /
avatar image
0
Question by Buelau · Nov 19, 2015 at 01:00 PM · c#scripting problem2d gamescripting beginnertime.deltatime

Can somebody help me fix this script?

Im trying to get the following script to work but it doesn't seem to work like it should. I want it to charge a shot and fire it with the charged force, in the moment it does not charge for some error in the script. Any help/tips is appreciated! code:

 public Transform firepoint;
 public GameObject pumpkin3;
 public float accelaration;
 public float multiplicator;
 private float shotdelay;
 private float Nextfire = 0.0f;
 float speed;
 public float chargetime = 0.0f;
 bool charging; 

 void start()
 {
     charging = false;
     multiplicator = 5f;
     shotdelay = 0.5f;
 } 
  
 void Update()
 {
     if (Input.GetMouseButtonDown(0))
     {
         charging = true;
         speed = 0f;
         chargetime = 0f;
     }

     if (charging == true)
     {
         chargetime +=Time.deltaTime * multiplicator;
         speed = chargetime;
     }
     if (Input.GetMouseButtonUp(0))
     {
         GameObject Go = Instantiate(pumpkin3, firepoint.position, firepoint.rotation) as GameObject;
         Go.GetComponent<Rigidbody2D>().AddForce(new Vector2(accelaration * speed, accelaration));
         Nextfire = Time.time + shotdelay;
         charging = false;
     }
     if (Time.time > Nextfire)
     {
         chargetime = 0;
         speed = 0;
     }
 }
 }

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 _Game_Dev_Dude_ · Nov 19, 2015 at 02:23 PM

 if (Time.time > Nextfire)
      {
          chargetime = 0;
          speed = 0;
      }


This right here is probably your problem. I don't know what you are trying to do with this block of code but what it is effectively doing is reseting your chargetime and speed every frame EVEN if you are currently charging.

I also don't see anywhere that you are changing your acceleration variable so unless you are editing it in the inspector, it will be 0 and will mess up your addforce code. Hope this helps.

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 Buelau · Nov 19, 2015 at 02:46 PM 0
Share

Thank you for the answer! yes the accelaration variable is changed in the inspector.

avatar image
0

Answer by Landern · Nov 19, 2015 at 02:25 PM

Well, your "start" method should have an upper case "S", that would stop your initialization of some of your variables.

You should probably use if else statements since your fall through if statements are getting hit probably when you don't want. An example is the first if that checks to see if the mouse button is down, if it is it sets charging to true, which is fine, this also makes the next if statement true and then in the next frame the same thing happens over and over resetting your variables like you just pressed the mouse button for the first time, particularly because your time and speed variables are reset to 0 every time a frame is rendered.

You may need to adjust your NextFire variable, but this should get you going:

 void Update()
 {
     if (Input.GetMouseButtonDown(0) && charging == false)
     {
         if (Time.time > Nextfire)
         {
             charging = true;
             speed = 0f;
             chargetime = 0f;
         }
     } 
     else if (Input.GetMouseButtonDown(0) && charging == true)
     {
         chargetime +=Time.deltaTime * multiplicator;
         speed = chargetime;
     }
     else if (Input.GetMouseButtonUp(0) && charging == true)
     {
         GameObject Go = Instantiate(pumpkin3, firepoint.position, firepoint.rotation) as GameObject;
         Go.GetComponent<Rigidbody2D>().AddForce(new Vector2(accelaration * speed, accelaration));
         Nextfire = Time.time + shotdelay;
         charging = false;
     }
 }
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 Buelau · Nov 19, 2015 at 02:55 PM 0
Share

Thank you! I changed my script to this and tried it in unity but it still doesn't seem to charge, but I noticed that the chargetime variable is not changing at all, like before, so maybe it has something to do with that? I also adjusted the Nextfire variable, which works now. Or do I need to charge the accelaration variable, like Game_Dev_Dude said? I'm sorry if these questions may seem like dumb questions, but I'm still very new to scripting (started 5 days ago).

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

38 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

Related Questions

How to use value from a function in another function 1 Answer

Player not moving in the right direction instantly 1 Answer

Issue toggling Particle system on/off using bool. 1 Answer

Adding a Highscore to game 1 Answer

Keeping Score out of Update Function 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