- Home /
foosball game - velocity problem
Hello!
I'm exploring unity and trying to develop a little game, something along the lines of a table football (foosball) game in the away it behaves.
The issue is when an object (in this case, a ball) hits the player object.
I'm using the following code attached to the ball collider.
if(hit.gameObject.tag =="player") {
var mag3=rigidbody.velocity.magnitude; rigidbody.AddForce(Vector3.Reflect(rigidbody.velocity*mag3,-Vector3.forward), ForceMode.Impulse); }
}
The ball is a rigidbody and the object player a boxcolider. So my doubt is how do i transfer the energy of the player (imagine that the player rotates 90 in one direction, and then quickly in the opposite direction, hitting the ball) to the ball, so that the ball velocity will be affected by the velocity of the player.
Thanks for any help, i trying to solve this problem to make it seems realistic but its have been a a little beat difficult to me.
Answer by duck · Feb 09, 2010 at 01:40 PM
If you are actually spinning the player object rigidbodies around, you shouldn't need to do any of the calculations of force and energy transfer yourself. This is what the physics engine is designed to do for you.
You should find that all you need to do is create and adjust the physics materials so that the collision results respond the way that you'd like them.
Is there any specific reason that you're trying to add some extra force in addition to the forces which occur as a natural result of the collision when the ball hits the player?
Edit:
The problem may be that you have the player set as "Kinematic". Try turning that off, and use the physics engine to rotate it. You could do this either by applying torque, or by adding a configurable joint and setting the target angle.
For more information about using a configurable joint to control rotation, see this question and answer:
hi Ducks!
Yes, well i thought that once the ball movement after hitting the player is really slow i will need to add an extra force, and this force should be related to the velocity that the player is spinning. Does the physics engine do that for him self? I dont need to add any force? I have the player whit a rigidbody attached , is kinematic: selected, use gravity- deselected , material: $$anonymous$$etal, and a mass:50. The ball: rigidbody attached, gravity-selected, material: Wood, mass:1;
Thanks in advance!
(added more detail to the answer, based on your mention of 'is kinematic' being selected)
Hi. Thanks again for you answer. Just one question, if I don't have the "is kinematic" selected the player rigidbody decompose it self (arms, legs body start to move in space). As far as I know i need a rigidbody attached so I can play with the object mass, right? greetings
Yes, if the arms and legs are separate gameobjects, make sure they're parented to the root player object, and have a rigidbody attached to just the root player object.
Answer by Ashkan_gc · Feb 09, 2010 at 04:33 PM
we are making a foosball too. just create a normal rigidbody and attach a box collider to it and use a joint to connect it to the bar and then rotate it with forces and it should do what you want well.
hey! thanks for you help. What kind of join do you use? Why do you use join ins$$anonymous$$d of normal parenting? Thanks
we use configurable joint that has just one axis rotation allowed arround the x axis and can just move along the x axis too. then we use springs to rotate it. it automatically rotate back to it's initial position and it has a really good gameplay. test it.
thanks a lot, really. sorry to bored but if you could me answer me just one more question. did you had any troubles when you move the bar horizontally and the player collides with the walls of the table? my start to jump, and i just have a box collider associated to the wall:S
Your answer
Follow this Question
Related Questions
Collision Issue - Walk Through Object 0 Answers
Simple Movement Game: Physics vs Manual Collision Detection? 2 Answers
Vehicle Rigidbody Movement / Collision 1 Answer
Calling Rigidbody.MovePosition, no movement at all 4 Answers
Player getting stuck in ground (3D) player has Rigidbody, and Box Collider, world is Mesh Colliders 0 Answers