this code slows down how to optimize it

float speedPlayer=2.0f;
speedObj= _rb.velocity.magnitude;
if(speedObj>=speedPlayer){
        _rb.velocity=_rb.velocity.normalized*speedPlayer;
      }

That code wont be slowing you down. How are you executing it? How many objects are running it?

1 Like

You understand that code is intended to cap your speed, right, i.e., slow it down?

the translator is out of order, sorry, you need to fix the code so that the FPS does not drop, but when you run the code, the FPS becomes small

Have you used the Unity profiler to check where your performance problem is? I doubt that the code you have presented here is causing the problem.

https://docs.unity3d.com/Manual/Profiler.html
https://docs.unity3d.com/Manual/ProfilerCPU.html

1 Like

I’ll just quote myself here. Please tell us more about how you execute this code (possibly by showing the entire script). And tell us how many of these scripts run in your scene. The code, as posted, will not drop your FPS. Not even if you run it on a literal pocket calculator. So you either have hundreds or thousands of objects executing this, or do so in a loop, or inflate the cost in some other way. We have too little information to say anything about your problem.

Also, how did you check that these lines specifically drop your FPS? Did you profile it?

1 Like

this piece of code later gets the speed of the object from rigitbody
puts this value into the variable speedObj and compares this variable with playerSpeed, if it is greater than speedObj, decreases the value of this variable to playerSpeed,
and this operation heavily loads the computer

thanks, I’ll try

I think this is a language problem, but that answers none of my questions. Nor does it provide any new information at all. You just described what the piece of code you already posted does and repeated what you think the problem is. Not to sound offensive, but i can read myself.

The piece of code you provided consists of one assignment, calculating magnitude once, comparing two values, one multiplication, and assigning one more value. None of these operations are expensive, and all in all the runtime of this piece of code is O(1), meaning constant. This wont affect your FPS. I was not joking when i wrote you could literally run this on a pocket calculator.

Since this code is not the problem (by itself) i was asking if you maybe are running it in a loop, or have hundreds / thousands of objects running this code. You did not answer these questions.
In your reply please include the following:

  • The entire script containing the above code sniplet
  • Tell us how many objects run this code, ie how many objects have this script attached in your active scene
  • Add any additional information you may have found out using the profiler so far, as suggested by PraetorBlue. Optionally explain how you even arrived at the conclusion that the posted code sniplet is causing the problem, since you seemingly did not use the profiler before.

What the others said but also:
Are you running this in Update or in FixedUpdate? (Should be FixedUpdate since it is interacting with Physics)
Are you measuring and displaying FPS or do you mean that the movement looks laggy/jittery? Because just capping the speed like this could cause jittery movement at perfectly smooth framer rate, depending on what else and when in the frame, is affecting the velocity.

Also, do you have a camera following this object and that appears to be jittery? Try updating it’s position in LateUpdate.

But yes, try using the Profiler. If the profiler doesn’t hint at a slow frame rate, you have a logic and update order issue, not a performance issue.

1 Like