- Home /
Make ball remain in front of player during player movement
HI am working on football game i have a character name lonhjhon tag name "player" and a ball tag name "ball" i need help regarding how to keep ball in front of player while player is moving forward or right left. (ball control remain in player until he kicks)
I try it by making ball as child to player but it just attached to it from start and it always contains the control, i want to make it if player move toward the ball the ball control will be giving to the that payer until player kicks it, i would be very grateful to you if some one helps me in this
I added rigid body to the ball, player runs fine and if it collide the ball it pushes it 2 or 3 times and than ball left behind,
A simple way to do this would be to make a variable, either on the player or on the ball, something like ballInControl. When the player has the ball, set that to true. Then, just check each frame, if the ball is in the players control, set balls position to player position + some offset in front of the player. When the player kicks the ball, set ballInControl to false.
We would need to see the code you have to be able to write a specific answer.
Answer by Muhwu · Dec 08, 2014 at 10:08 AM
If you want the ball to just stick to the player, you can add some sort of joint (and it might behave neatly - for example distance joint). If you want the ball to stay in the position where it touched the player the first time, you can implement OnCollisionEnter(2D?) event so that it stores the relative position of the ball to the player on hit. Then, on every update, you can set the ball position to be the same relative to the player position.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Problems with Transform.Find and GameObject.find 1 Answer
new Line in a CustomPropertyDrawer 1 Answer
Simple Timer 2 Answers