x


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.

more ▼

asked Feb 09 '10 at 12:13 PM

bOrange gravatar image

bOrange
27 3 4 8

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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:

Keyboard Controlled Configurable Joint

more ▼

answered Feb 09 '10 at 01:40 PM

duck gravatar image

duck ♦♦
41k 92 148 415

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: Metal, and a mass:50. The ball: rigidbody attached, gravity-selected, material: Wood, mass:1;

Thanks in advance!

Feb 09 '10 at 02:46 PM bOrange

(added more detail to the answer, based on your mention of 'is kinematic' being selected)

Feb 09 '10 at 04:50 PM duck ♦♦

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

Feb 10 '10 at 11:32 AM bOrange

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.

Feb 10 '10 at 03:33 PM duck ♦♦

thanks duck, it was very helpful !

Feb 10 '10 at 05:57 PM bOrange
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Feb 09 '10 at 04:33 PM

Ashkan_gc gravatar image

Ashkan_gc
9.1k 33 56 117

hey! thanks for you help. What kind of join do you use? Why do you use join instead of normal parenting? Thanks

Feb 10 '10 at 04:41 PM bOrange

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.

Feb 10 '10 at 05:03 PM Ashkan_gc

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

Feb 10 '10 at 07:12 PM bOrange
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2503
x1879
x1371

asked: Feb 09 '10 at 12:13 PM

Seen: 1810 times

Last Updated: Feb 10 '10 at 03:06 PM