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);
}
}
}