0

I am developing a game in which I have a ball I have to throw. It must be able to collide with other balls (I have 10 for my demo). Right now, after colliding, the ball that has been hit is continuously moving and not stopping.

My solution for this problem is to enable isKinematic for some time and then disable it. I access all the balls with a tag with code like this:

GameObject[] marbles;
marbles = GameObject.FindGameObjectsWithTag ("Exit");
foreach (GameObject x in marbles) {
     x.GetComponent<Rigidbody> ().isKinematic = true;
}

My problem is that this only partially enables the objects. It enables some of the objects and disables the rest of the GameObjects. What is the problem with this code?

My boss told me to put a script on all the balls and then stop the balls by the same method (isKinematic is true for some time), but I think it will lower the fps of the game if I use too many instances of one script on each gameObject. Which approach is better?

Servy
  • 202,030
  • 26
  • 332
  • 449
  • Are those balls instantiated during the game or did you set them all up in the scene? – I.B Apr 14 '17 at 13:09

1 Answers1

1

As per your statement:

after colliding the hitted ball continuously moving and not stopping so my solution for that problem is to enable isKinematic for sometime and then disable it

I have few questions and suggestion that might help you:

  • Have tried to add materials (with some friction value) to the moving objects that never stops ?

This should help decrease the ball velocity and will let the ball stop after some time based on friction value.

  • Also have you let the movements of the ball controlled by physics engine or you have applied force to these moving objects on collision event ?

If you are controlling the velocity of moving balls and it is constantly updating the velocity in like Update function it will never stop. Let the physics judge the moving ball's velocity, direction and duration of movement.

NeverHopeless
  • 11,077
  • 4
  • 35
  • 56
  • Thanks for Answer! From the first bullet i think you said about physics material, i have added that already and get the property of glass in it.. I dont think so that i am right or wrong because one is dynamic friction which is 0.4 and static friction which is 0.94.. –  Apr 14 '17 at 11:22
  • regarding friction value could please see this video how the it can be vary to control the friction in any direction: https://unity3d.com/learn/tutorials/topics/physics/physics-materials About physics engine i mean you add force/apply velocity to a game object (http://answers.unity3d.com/questions/636566/addforce-with-2d-character.html) instead of doing transformation in update, applied force will be handle by physics effect but transformation in update like this (http://stackoverflow.com/questions/22467674/move-simple-object-in-unity-2d) doesn't know when to stop unless define. – NeverHopeless Apr 14 '17 at 11:39