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 ContrerasJose · Mar 12 at 11:33 PM · scripting problembeginner

How do I call a function that increments and returns a value from the Game Manager Script to another?

Hi everyone. I am trying to get back into game programming after a long absence so I am currently working on a ping pong game, where for every second you hold down an arrow key to move your paddle the faster the paddle will move. I want this to occur by adding an incrementing momentum value to increase the paddle's speed. The problem is that when I debug log to see the momentums value it stays at "1" when it should be increasing to a max of "10" every second that the paddles are moving. Since this does not happen the paddles do not speed up while they move as they should. I would appreciate feedback on what I am doing wrong and what I could be doing better. Thank you

public class GameManager : MonoBehaviour {

public static float paddleSpeed = 1.0f;

public static float topWall = 4.49f;

public static float bottomWall = -4.49f;

public static float momentum = 1.0f;

public static float momentumGain = 1.0f;

public static float maxMomentum = 10.0f;

 public float addPaddleMomentum(float momentum, float momentumGain, float maxMomentum)
 {
     if (momentum < maxMomentum)
     {
         momentum += momentumGain * Time.deltaTime;
     }
     else
     {
         momentum = maxMomentum;
     }
     return momentum;
 }

}

public class UserPaddleController : MonoBehaviour {

private float userPaddleSpeed;

private float baseUserPaddleMomentum;

private float userPaddleMomentum;

private float userPaddleMomentumGain;

private float maxUserPaddleMomentum;

private float topUserPaddleBoundary;

private float bottomUserPaddleBoundary;

public GameManager gms;

 // Start is called before the first frame update
 void Start()
 {
     userPaddleSpeed = GameManager.paddleSpeed;
     topUserPaddleBoundary = GameManager.topWall;
     bottomUserPaddleBoundary = GameManager.bottomWall;
     baseUserPaddleMomentum = GameManager.momentum;
     userPaddleMomentumGain = GameManager.momentumGain;
     maxUserPaddleMomentum = GameManager.maxMomentum;
 }

 // Update is called once per frame
 void Update()
 {
     //Setting boundaries to keep the paddles in game screen
     transform.position = new Vector2((transform.position.x), Mathf.Clamp(transform.position.y, bottomUserPaddleBoundary, topUserPaddleBoundary));

     //Section controlling upward movement and speed of player paddle
     if (Input.GetKey(KeyCode.UpArrow))
     {
         userPaddleMomentum = gms.addPaddleMomentum(baseUserPaddleMomentum, userPaddleMomentumGain, maxUserPaddleMomentum);

         this.transform.Translate(Vector2.up * userPaddleSpeed * userPaddleMomentum * Time.deltaTime);;
     }

     //Section controlling downward movement and speed of player paddle
     if (Input.GetKey(KeyCode.DownArrow))
     {
         userPaddleMomentum = gms.addPaddleMomentum(baseUserPaddleMomentum, userPaddleMomentumGain, maxUserPaddleMomentum);

         this.transform.Translate(Vector2.down * userPaddleSpeed * userPaddleMomentum * Time.deltaTime);
     }
 }

}

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 kaushiknis · Mar 15 at 07:44 PM

@ContrerasJose

The issue is in the update function of "UserPaddleController" class. You have to pass the latest momentum to the addPaddleMomentum method. You was passing the same baseUserPaddleMomentum which is 1 by default and never changes. Thus, the calculation happen in the method remains same always. Pasting the update method code for UserPaddleController class. Also you need to reset the userPaddleMomentum to get the accelerating effect every time. Hope this helps.

    void Update()
     {
         bool inMotion = false;
         //Setting boundaries to keep the paddles in game screen
         transform.position = new Vector2((transform.position.x), Mathf.Clamp(transform.position.y, bottomUserPaddleBoundary, topUserPaddleBoundary));
         //Section controlling upward movement and speed of player paddle
         if (Input.GetKey(KeyCode.UpArrow))
         {
             inMotion = true;
             userPaddleMomentum = gms.addPaddleMomentum(userPaddleMomentum, userPaddleMomentumGain, maxUserPaddleMomentum);
             this.transform.Translate(Vector2.up * userPaddleSpeed * userPaddleMomentum * Time.deltaTime); ;
         }
         //Section controlling downward movement and speed of player paddle
         if (Input.GetKey(KeyCode.DownArrow))
         {
             inMotion = true;
             userPaddleMomentum = gms.addPaddleMomentum(userPaddleMomentum, userPaddleMomentumGain, maxUserPaddleMomentum);
             this.transform.Translate(Vector2.down * userPaddleSpeed * userPaddleMomentum * Time.deltaTime);
         }
 
         // If user is not pressing any key, reset paddle momentum.....
         // Can write logic to decrement stepwise
         if (!inMotion)
         {
             userPaddleMomentum = baseUserPaddleMomentum;
         }
 
         Debug.Log("momentum: " + userPaddleMomentum);
     }

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 ContrerasJose · Mar 17 at 03:29 AM 0
Share

Thanks that did work. I thought I needed to pass in the base momentum because I set up the function to accept the base momentum and make the calculations with it. Thanks again for taking the time to reply.

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

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

Character gets stuck where he spawns??? 1 Answer

,DontDestroyOnLoad Object spawns endlessly 1 Answer

How do I send a variable (after calculation) as the final variable to another game object 1 Answer

Resources.Load With Scriptable Objects 1 Answer

I'm following a tut - converting to 3d - and confuzzled by RigidBody2d - vector 3 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