This code (magnet) gives 2 -3 fps when turned on, without it - 70 fps. Provided that the magnet attracts 2-3 rigidbody. Without objects near it does not hang at all, with one object - 60 fps, with two or three drops to 3 fps;

public class MAGNETO : MonoBehaviour { public float radiusdal; Transform mytr; public float forcedal; // Use this for initialization void Start() { mytr = transform; } // Update is called once per frame void FixedUpdate() { Collider[] coldal = Physics.OverlapSphere(mytr.position, radiusdal); foreach (var col1 in coldal) { //Π’ΠΎΡ‚ Ссли этот кусок ΠΊΠΎΠ΄Π° ΡƒΠ±ΠΈΡ€Π°Π΅ΠΌ - ΠΌΠ°Π³Π½ΠΈΡ‚ Π½ΠΈΡ‡Π΅Π³ΠΎ Π½Π΅ притягиваСт, Π½ΠΎ ΠΈ Π²ΠΎΠΎΠ±Ρ‰Π΅ Π½Π΅ Ρ‚ΠΎΡ€ΠΌΠΎΠ·ΠΈΡ‚. if (col1.attachedRigidbody != null) { var ux = Vector3.Distance(mytr.position, col1.transform.position); var aux = 0.5f / (ux); col1.attachedRigidbody.AddForce((mytr.position - col1.transform.position)* aux* forcedal, ForceMode.VelocityChange); }// Π·Π°ΠΊΠΎΠ½Ρ‡ΠΈΠ»ΠΈ тормозящий кусок ΠΊΠΎΠ΄Π°. } } } 

collider.attachedRigidbody was taken as a replacement for GetComponent<Rigidbody> , but the result is exactly the same: 2-3 fps.

  • one
    Judging by the number of spaces in the code, you cut something out so as not to show it. But I feel the snag is somehow not connected with the piece that you have marked out with a comment. Or coupled with everything else that is written. From one such code - it should not slow down - Alexey Shimansky
  • one
    By the way, during your OverlapSphere recording, your magnet will also get into the OverlapSphere array, since you have assigned his position to the center. Maybe (and vdruuuuuuuug) the point is this and you need to write at least if (col1.attachedRigidbody != null && col1.gameObject.name != "myMagnetObjectName") { ....... although without this you should get an error , but apparently you ignore the error)) - Alexey Shimansky
  • @ Alexey Shimansky actually the magnet itself is not a rigid body. To not move and just pulled. And nothing is removed from the code in fixedupdate, and at the start it does not slow down - only if there are several objects in the field. The catch may be that some of these bodies have their own addforce scripts - they must pursue a goal. But by themselves they do not hang, even if 10 pieces on stage. But if you put a magnet .. - Dmitrii
  • one
    Well, this code itself will not slow down either. To test myself shoved it - it works as it should. so somewhere everything together slows down ... Π½Π΅ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Π΅ ΠΈΠ· этих Ρ‚Π΅Π» ΠΈΠΌΠ΅ΡŽΡ‚ собствСнныС скрипты с addforce - ΠΎΠ½ΠΈ Π΄ΠΎΠ»ΠΆΠ½Ρ‹ ΠΏΡ€Π΅ΡΠ»Π΅Π΄ΠΎΠ²Π°Ρ‚ΡŒ Ρ†Π΅Π»ΡŒ and who should they pursue? show their code. maybe the truth is with them what is happening ..... And so - the problem is not reproducible - Alexey Shimansky
  • @ AlekseyShimanskiy Here is the code from the pursuing bodies void Start () { rb = GetComponent<Rigidbody>(); myTransform = transform; } // Update is called once per frame void FixedUpdate () { myTransform.LookAt(player.transform.position); rb.AddForce(myTransform.forward * enemy1speed, ForceMode.Force); } void Start () { rb = GetComponent<Rigidbody>(); myTransform = transform; } // Update is called once per frame void FixedUpdate () { myTransform.LookAt(player.transform.position); rb.AddForce(myTransform.forward * enemy1speed, ForceMode.Force); } void Start () { rb = GetComponent<Rigidbody>(); myTransform = transform; } // Update is called once per frame void FixedUpdate () { myTransform.LookAt(player.transform.position); rb.AddForce(myTransform.forward * enemy1speed, ForceMode.Force); } - Dmitrii

1 answer 1

I did it, everything works fine:

 public float Radius = 5f; public float ForeceScale = 1f; void FixedUpdate () { foreach (Collider item in Physics.OverlapSphere (transform.position, Radius)) { if (item.attachedRigidbody) { float ux = 0.5f / Vector3.Distance (transform.position, item.transform.position); item.attachedRigidbody.AddForce ((transform.position - item.transform.position) * ux * ForeceScale, ForceMode.Impulse); } } }