pong paddle issues

I’m making a pong game,and my ball and paddles keep going through the border.I have already tried the empty object trick,and I have also tried using the following code:

transform.position.y=Mathf.Clamp(transform.position.y,0, 100);

However, when using the above, my paddles no longer move =(

Are there any other ways to keep this from happening? Also,here is my current code for the player 1 (player 2 is the same, except for different key binders):

function Update () {
if(Input.GetKey(“up”)){
transform.Translate (0,4 * Time.deltaTime,0);
}

if(Input.GetKey(“down”)){
transform.Translate (0,4 * Time.deltaTime,0);
}
}

and the code for the ball mover logic:

function Start () {
rigidbody.freezeRotation = true;
yield WaitForSeconds (2);
rigidbody.AddForce (Random.Range(-4.1,4.1) , Random.Range(-4.1,4.1) ,0);
}

function Update () {
transform.position.z = 0;
}

Maybe there is something wrong with them? Please help!

FYI I am using this page for reference:

Thanks!

Kenzie

Hi, welcome to the forum!

Rather than use transform.Translate, you need to calculate the offset from the current position and then clamp the result:-

function Update () {
	if(Input.GetKey("up")){
		transform.position.y = Mathf.Clamp(transform.position.y + 4 * Time.deltaTime, 0, 100);
	}	
	
	if(Input.GetKey("down")){
		transform.position.y = Mathf.Clamp(transform.position.y - 4 * Time.deltaTime, 0, 100);
	}
}

This solved my problem!Thanks!And I’d do the same thing for the ball,right?