High FPS and rocket rotates slowly

Hi everybody,

i realized that when i hit play and take a look at the stats, the FPS value shown is super high (around 1700 on average!).
If i activate vsync though, FPS go down to around 70 which seems more normal to me idk.

Btw, i am doing everything as the istructor is, but using

transform.Rotate(0, 0, 1 * Time.deltaTime);

the rocket rotate very slowly, definitely not as the instructor shows.

[edit] if i activate vsync i need to set even the thrust force to at least 10 to have a decent boost. With 2000 FPS 1 is enough to fly.

Does any of you have the same issue?

Thanks

Valerio

Hi Valerio,

You could try to move the code for the rotation to the FixedUpdate method, without Time.deltaTime. I wrote a similar solution for the thrusting. You just have to apply it to the rotation code.

bool isThrusting = false;

private void RespondToThrustInput()
{
  isThrusting = Input.GetKey(KeyCode.Space);

  if (isThrusting)
  {
    ApplyThrust();
  }
  else
  {
    audioSource.Stop();
    mainEngineParticles.Stop();
  }
}

private void ApplyThrust()
{
  if (!audioSource.isPlaying)
  {
    audioSource.PlayOneShot(mainEngine);
  }

  mainEngineParticles.Play();
}

private void FixedUpdate()
{
  if (isThrusting)
  {
    rigidBody.AddRelativeForce(Vector3.up * mainThrust, ForceMode.Acceleration);
  }
}

Did this fix the issue?

Hi Nina,

I tried to used FixedUpdate but the result is still exactly the same. In order to make the rocket ship move i need to use high values of force and rotate, when the instructor flies just with one.

Here is my FixedUpdate test for reference:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Rocket : MonoBehaviour
{
    // Create a rigidbody object reference by setting a variable
    Rigidbody rigidBody;

    public bool isBarPressed = false;
    public bool isLeftAPressed = false;
    public bool isRightAPressed = false;



    // Start is called before the first frame update
    void Start()
    {
        rigidBody = GetComponent<Rigidbody>();
    }

    // Update is called once per frame
    void respondToCommands()
    {
        isBarPressed = Input.GetKey(KeyCode.Space);
        isLeftAPressed = Input.GetKey(KeyCode.LeftArrow);
        isRightAPressed = Input.GetKey(KeyCode.RightArrow);
    }

    void Update()
    {
        respondToCommands();
    }

    void FixedUpdate()
    {
        print(isBarPressed);
        if (isBarPressed)
        {
            print("Pressed Bar");
            rigidBody.AddRelativeForce(0, 1000 * Time.deltaTime, 0);
        }
        if (isLeftAPressed)
        {
            print("Pressed Left");
            transform.Rotate(0, 0, 100 * Time.deltaTime);
        }
        if (isRightAPressed)
        {
            print("Pressed Right");
            transform.Rotate(0, 0, -100 * Time.deltaTime);
        }
    }
}

Thanks,
Valerio
1 Like

Your code looks fine so far, apart from one thing: Time.deltaTime in FixedUpdate. It must not be used there because FixedUpdate gets called every x seconds and does not depend on the framerate.

Remove Time.deltaTime, then your code should hopefully work as expected. :slight_smile:

Thanks Nina,

i’m just setting on different values for my forces and rotation, as you said it might be the different version of unity or who know what else.

I’ll put my heart to piece now and proceed with the rest of the course.

Thanks again,
Valerio

If different values fix the issue for you, use different values. That’s perfectly fine. Nevertheless, I strongly recommend to remove Time.deltaTime from FixedUpdate. Adjust the values again if necessary.

rigidBody.AddRelativeForce(0, 1000 * Time.deltaTime, 0); should say something like rigidBody.AddRelativeForce(0f, speed, 0f);. And then you could declare a float variable named speed at the top of your code and assign a value which is higher than 0f. Do the same for the rotation code. Declare more variables if you need individual speed values.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms