- Home /
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);
}
}
}
Answer by kaushiknis · Mar 15 at 07:44 PM
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);
}
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
Follow this Question
Related Questions
How do I instantiate/fire when Joystick input ends? 0 Answers
OnTriggerEnter and UI Collisions. 0 Answers
Instantiate not working? 0 Answers
NavMeshAgent, goes to the wrong place 0 Answers