- Home /
How to make two different objects share the same x value?
So I'm trying to make a menu screen for a simple arcade type game similar to breakout to practice as I'm new to unity. What I want is when on the menu screen you see a game being played by the computer. I'm trying to set the X position of the paddle to the X position of the ball so they always collide. What I have tried so far is this.
{ Rigidbody _rigidbody; public GameObject ball; void Start() { _rigidbody = GetComponent(); }
void Update()
{
_rigidbody.MovePosition(new Vector3(ball.transform.position.x, -17, 0));
}
}
Answer by Bicsi · Jun 28, 2021 at 10:46 PM
I havent worked alot with the Rigidbody component in Unity, but i see 3 different things you should try out. There is definitely more ways to go about it, so take this with a grain of salt.
You can try one of the following:
Vector3.MoveTowards if you still want to keep a certain smoothness as you would with a Rigidbody. This way you can still make the paddle move overtime.
If you want to keep using a rigidbody, perhaps you should look at using Rigidbody.AddForce. Using this you would simply need to choose a force you want to apply, and then you can quite simply use if functions to check for the balls x, y and z value relative to the paddle position, and add a force accordingly to your rigidbody.
Last thing you could do, if the physicsbased smoothness is not important to you, but simply want the functionality, you could set the paddles transform.position.x to be equal to the one of the ball.
Any of these should do the job just fine. The last one absolutely being the easiest to do. Let me know if this helps you :-)
Feel free to ask any further questions.
Your answer
![](https://koobas.hobune.stream/wayback/20220613035552im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
2D Object Movement, PingPong issues 0 Answers
why player jumps forever 2 Answers
Issue with attacking and animation in a 2D game 1 Answer
Enemy is moving opposite of the player 2D 1 Answer
Cross platform 2D camera 1 Answer