The program pulls the cursor behind the rigidbody, the cursor shoots with a raikast. For some reason, during the movement, everything starts to slow down wildly. Without movement 72 fps, with it 6 fps. What is it and how to deal with it. GetComponent in Update like not.

The contents of the script that slows down. Disabled the rest - anyway, maximum 15 fps in motion.

public class RayCaster : MonoBehaviour { private Ray ray; private RaycastHit hit; public float speedphys; public GameObject player; Rigidbody rb; // Use this for initialization void Start () { rb = player.GetComponent<Rigidbody>(); } // Update is called once per frame void FixedUpdate() // Ρ‚Π°ΠΊ ΠΊΠ°ΠΊ ΠΌΡ‹ Ρ€Π°Π±ΠΎΡ‚Π°Π΅ΠΌ с Ρ„ΠΈΠ·ΠΈΠΊΠΎΠΉ Π² Unity, FixedUpdate ΠΏΡ€Π΅Π΄ΠΏΠΎΡ‡Ρ‚ΠΈΡ‚Π΅Π»ΡŒΠ½Π΅Π΅ ΠΎΠ±Ρ‹Ρ‡Π½ΠΎΠ³ΠΎ Update { //Под Ρ€Π°Π·Π½Ρ‹Π΅ ΠΏΠ»Π°Ρ‚Ρ„ΠΎΡ€ΠΌΡ‹ Π½Π°ΠΌ трСбуСтся Ρ€Π°Π·Π½Ρ‹ΠΉ ΠΊΠΎΠ΄. #if UNITY_EDITOR ForUnityEditorAndPC(); #endif #if UNITY_ANDROID ForAndro(); #endif #if UNITY_STANDALONE ForUnityEditorAndPC(); #endif } void ForUnityEditorAndPC() { if (Input.GetMouseButton(0)) { ray = Camera.main.ScreenPointToRay(Input.mousePosition); // Если Π΄Π΅Π»ΠΎ ΠΈΠΌΠ΅Π΅ΠΌ с ΠΊΠΎΠΌΠΏΡŒΡŽΡ‚Π΅Ρ€ΠΎΠ² - ΠΈΠ· ΠΌΡ‹ΡˆΠΊΠΈ Π»ΡƒΡ‡ пускаСм raycast(); } } void ForAndro() { if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) { ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position); // Если Π°Π½Π΄Ρ€ΠΎΠΈΠ΄ - Ρ‚ΠΎ ΠΈΠ· Ρ‚Π°Ρ‡Π°, ΠΊΠΎΠ³Π΄Π° ΠΎΠ½ двиТСтся. raycast(); } } void raycast()//Π­Ρ‚ΠΎΡ‚ ΠΊΠΎΠ΄ для ΠΎΠ±ΠΎΠΈΡ… устройств ΠΎΠ±Ρ‰ΠΈΠΉ, Π½Π΅Ρ‚ смысла Π΅Π³ΠΎ Π΄ΡƒΠ±Π»ΠΈΡ€ΠΎΠ²Π°Ρ‚ΡŒ. Π’ΡƒΡ‚ ΠΌΡ‹ смотрим, ΠΊΡƒΠ΄Π° ΡƒΠΏΠ°Π» Π»ΡƒΡ‡ { if (Physics.Raycast(ray, out hit) && !hit.collider.CompareTag("Player")) { var dir = (hit.point - player.transform.position); player.transform.DOLookAt(hit.point, 0.1f); Debug.Log(hit.point); rb.velocity = new Vector3(dir.x, 0, dir.z) * speedphys; //Π”Π²ΠΈΠ³Π°Π΅ΠΌ ΠΊΠΎΠ»Π»Π°ΠΉΠ΄Π΅Ρ€ ΠΊ мСсту ΠΊΡƒΠ΄Π° Π»ΡƒΡ‡ ΡƒΠΏΠ°Π». Rigidbody ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΡƒΠ΅ΠΌ, Ρ‚Π°ΠΊ ΠΊΠ°ΠΊ Ρ„ΠΈΠ·ΠΈΠΊΠ°, Π΄Π° ΠΈ силы слишком ΠΌΠ½ΠΎΠ³ΠΎ Π±Ρ‹Ρ‚ΡŒ //Π½Π΅ Π΄ΠΎΠ»ΠΆΠ½ΠΎ - Π° Ρ‚ΠΎ раскидаСт Ρ„ΠΈΡˆΠΊΠΈ ΠΎΡΡ‚Π°Π»ΡŒΠ½Ρ‹Π΅. Π‘ ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ΠΎΠΌ speedphys Π»Π΅Π³ΠΊΠΎ Ρ€Π΅Π³ΡƒΠ»ΠΈΡ€ΠΎΠ²Π°Ρ‚ΡŒ ΡΠΊΠΎΡ€ΠΎΡΡ‚ΡŒ ΠΊΠΎΠ»Π»Π°ΠΉΠ΄Π΅Ρ€Π° прямо //ΠΈΠ· Ρ€Π΅Π΄Π°ΠΊΡ‚ΠΎΡ€Π°, Π½Π΅ заглядывая лишний Ρ€Π°Π· Π² ΠΊΠΎΠ΄. Debug.Log(hit.point + " hitpoint"); //Debug.Log(transform.position + " transform.position"); } } } 

    1 answer 1

    Transferred the code from Update to Corutin. With 20 updates per second (0.05 seconds waiting in corutin) steady fps 70-80. The problem does not appear.

     void Start () { rb = player.GetComponent<Rigidbody>(); StartCoroutine(cur()); } // Update is called once per frame IEnumerator cur() // Ρ‚Π°ΠΊ ΠΊΠ°ΠΊ ΠΌΡ‹ Ρ€Π°Π±ΠΎΡ‚Π°Π΅ΠΌ с Ρ„ΠΈΠ·ΠΈΠΊΠΎΠΉ Π² Unity, FixedUpdate ΠΏΡ€Π΅Π΄ΠΏΠΎΡ‡Ρ‚ΠΈΡ‚Π΅Π»ΡŒΠ½Π΅Π΅ ΠΎΠ±Ρ‹Ρ‡Π½ΠΎΠ³ΠΎ Update { while (true) { yield return new WaitForSeconds(0.05f); //Под Ρ€Π°Π·Π½Ρ‹Π΅ ΠΏΠ»Π°Ρ‚Ρ„ΠΎΡ€ΠΌΡ‹ Π½Π°ΠΌ трСбуСтся Ρ€Π°Π·Π½Ρ‹ΠΉ ΠΊΠΎΠ΄. #if UNITY_EDITOR ForUnityEditorAndPC(); #endif #if UNITY_ANDROID ForAndro(); #endif #if UNITY_STANDALONE ForUnityEditorAndPC(); #endif } }