Problem with vertical moving

Can someone help me please? Somehow my plane is moving very slow vertically. But moving left and right looks okay. I checked my code multiple times, looks exactly same with Rick’s. What can I do to fix this…

My Code:


Inspector view:

Hi Gulzatt,

Please note, it’s better to copy/paste your code and apply the code fencing characters, rather than using screenshots. Screenshots are ideal for displaying specific details from within a game engine editor or even error messages, but for code, they tend to be less readable, especially on mobile devices which can require extensive zooming and scrolling.

You also prevent those that may offer to help you the ability to copy/paste part of your code back to you with suggestions and/or corrections, meaning that they would need to type a potentially lengthy response. You will often find that people are more likely to respond to your questions if you make it as easy as possible for them to do so.

Regarding your problem, does your spaceship (or its parent) have got a rigidbody component attached? And does that component have gravity enabled? To me, your ship looks as if it was falling down due to gravity.

Another thing you could check is the timeline. Make sure that the children of the root game object (Player Rig in Rick’s case) is not animated. Their properties must not be used in any keyframe.

Hope this helps :slight_smile:


See also:

Oh thank you so much for letting me know how to post a code! Will try to do it.

public class PlayerControls : MonoBehaviour

{

[SerializeField] float controlSpeed = 10f;

[SerializeField] float controlSpeedY = 100f;

[SerializeField] float xRange = 10f;

[SerializeField] float yRange = 7f;

[SerializeField] float positionPitchFactor = -2f;

[SerializeField] float controlPitchFactor = -10f;

[SerializeField] float positionYawFactor = 2f;

[SerializeField] float controlRollFactor = -20f;

float xThrow;

float yThrow;

void Update()

{

    ProcessTranslation();

    ProcessRotation();

}

void ProcessRotation()

{

    float pitchDueToPosition = transform.localPosition.y * positionPitchFactor;

    float pitchDueToControlThrow = yThrow * controlPitchFactor;

    float pitch = pitchDueToPosition + pitchDueToControlThrow;

    float yaw = transform.localPosition.x * positionYawFactor;

    float roll = xThrow * controlRollFactor;

    transform.localRotation = Quaternion.Euler(pitch, yaw, roll);

}

void ProcessTranslation()

{

    xThrow = Input.GetAxis("Horizontal");

    yThrow = Input.GetAxis("Vertical");

    float xOffset = xThrow * Time.deltaTime * controlSpeed;

    float rawXPos = transform.localPosition.x + xOffset;

    float clampedXPos = Mathf.Clamp(rawXPos, -xRange, xRange);

    float yOffset = yThrow * Time.deltaTime * controlSpeedY;

    float rawYPos = transform.localPosition.y + yOffset;

    float clampedYPos = Mathf.Clamp(rawYPos, -yRange, yRange);

    transform.localPosition = new Vector3(clampedXPos, clampedYPos, transform.localPosition.z);

}

}

And thank you for the suggestion on my game.
I tried to see if there is some rigidbody or animation, but there is no such things in my game. I don’t know what else could work, but I just added another new variable for vertical control speed(

Maybe the problem is caused by the user input. Have you already tried to add Debug.Logs to your code to see what is going on during runtime? You could check what values these two lines “produce” if you press no key while the game is running:

xThrow = Input.GetAxis("Horizontal");
yThrow = Input.GetAxis("Vertical");

If the values are not 0, there might be a hardware problem. Check if you have a gamepad or if your keyboard is dirty.

I checked it out, and yeah it prints value 0 when I don’t press anything. But anyways, thank you for your help, appreciate it!

I pasted your code into a test project and the movement looks fine. I think there may be something else on your ship game object that’s giving you issues. Like @Nina mentioned, there certainly appears to be gravity being applied to your ship.

Note I did change the code to use controlSpeed and not the controlSpeedY value.

Thank you for your effort to help! I changed my code back so it looks exactly same with Ricks, and I checked the gravity, looks like it’s not checked, which is right…
Can you check if something is wrong here by any chance, please?

Your Rigidbody component looks fine.

Did you log the control values into your console? What values to you get when pressing your keys? Since your code seems to be fine, too, the problem might be caused by the user input.

OMG it suddenly started working! So the values were able to reach maximum 0.12 when the key is pressed shortly. But now with a short click it can reach 0.35, and started moving way faster. I didn’t do any changes, but it’s working now lol.

Which variable did you log into your console? The user input value should be 1 if you keep the key pressed down. If you don’t get that value, there might be an issue with the user input, either on Unity’s side or with the hardware.

I logged y variable into the console, and when I kept pressing it, the value eventually went to 1 and -1, and then tried x variable, both same, went to 1 and -1.

Great. This means that the user input work as expected.

In your code, you initialised the controlSpeedY variable with 100f. What value do you use in the Inspector? The value in the Inspector gets used at runtime.

The offsets get calculated here:

float xOffset = xThrow * Time.deltaTime * controlSpeed;
float yOffset = yThrow * Time.deltaTime * controlSpeedY;

Since xThrow and yThrow are 1 or -1 while keeping a key pressed down, the problem is very likely controlSpeedY.

@Nina the controlSpeedY is a value that was added to overcome the original issue. Ideally we would want to fix this without the use of this value at all because the course uses controlSpeed for both the xOffset and yOffset

My question is: Did controlSpeedY improve the behaviour or fix the problem? If there is no difference, there might be a big problem in the game, for example, a bug in the Unity engine. If it did improve the behaviour, the problem might be caused by the physics simulation.

The more we know about the problem and what improves/fixes the problem, the more likely we’ll be able to find the root of it. At the moment, we still do not know what might be causing it in the first place. “Use Gravity” is not checked in Gulzatt’s screenshot, so the gravity is probably not causing the problem.

1 Like