I'm creating a Pong clone inside of Unity and was trying to figure how to copy another object (In this case being the Y co-od of the Ball Object) How would i go about doing this? I'm using vectors as my method of paddle movement and I want to create a public int AISpeed so that the AI paddle is a little slower in comparison

In my mind I assumed it was along the lines of: rightPaddle.location.y = pongBall.location.y

How would I create this?


Assuming that rightPaddle and pongBall are both game objects, your code would look something like the following:

Vector3 paddleOldPos = rightPaddle.transform.position;
Vector3 ballPos = pongBall.transform.position
Vector3 newPos = new Vector3(paddleOldPos.x, ballPos.y, paddleOldPos.z);

rightPaddle.transform.position = newPos;

You can't modify a transform's position directly, you'll have to overwrite it, as above.

Hope that helps :-)


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.