Argon Assault game: Ship sliding to top left on it's own

Hello everyone. I’m doing the Unity “Complete C# Unity game developer 3D”, and reaching lesson 78, i got a very strange error, that appears randomly: without pressing any navigation key, the ship starts sliding to the top left corner. It responds to other keys, like down and right, but when i release them, it returns to the top left
image

In a course i had before, the teacher said this is caused by the mouse not being over the game window, so the input for the keys would be null (or the default value; don’t remember). But this time, i:
1- click game window
2- dont pass any other window, before pressing the play button
3- move the mouse back to the game window, and leave it there during the game

And randomly this still happens. I thougt, to prevent this there would be a way to force the focus on the game window, but had no luck so far. I dont upload the assets here, coz it seems too much: its 1.8 gb. Any thoughts?

this is the interface created by the [SerializeField] fields in the Playership prefab GameObject, with the overrides already applied

image

PlayerControls.cs

using UnityEngine;

public class PlayerControls : MonoBehaviour
{
    // velocidade
    [SerializeField] float controlSpeed = 10f;

    // amplitude de coordenadas para posição
    [SerializeField] float xRange = 10f;
    [SerializeField] float yRange = 5.93f;

    [SerializeField] float positionPitchFactor = -2f; // multiplicador para input "Down" do user
    [SerializeField] float controlPitchFactor = -11.46f; //  multiplicador para rotação pitch
    [SerializeField] float controlYawFactor = 3.23f;

    float xThrow, yThrow; // passamos a declarar aqui, pq tb as vamos usar na rotação

    void Update() {
        ProcessTranslation(); // posição 
        ProcessRotation(); // rotação

    }

    void ProcessRotation() {
        // pitch - rotação Y, roda para cima / baixo

        // position a impactar pitch
        float pitchDueToPosition = transform.localPosition.y * positionPitchFactor;

        // control input a impactar pitch
        float pitchDueToControlThrow = yThrow * controlPitchFactor;

        float pitch = pitchDueToPosition + pitchDueToControlThrow;

        // yaw - rotação em X, roda para os lados
        float yawDueToPosition = transform.localPosition.x * controlYawFactor;
        float yawDueToControlThrow = xThrow * positionPitchFactor;
        float yaw = yawDueToPosition + yawDueToControlThrow;

        float roll = 0f;
        transform.localRotation = Quaternion.Euler(pitch, yaw, roll);
    }

    // passamos pra esta funç, o processamento da posição
    void ProcessTranslation() {

        // Eixo X
        xThrow = Input.GetAxis("Horizontal");
        float xOffset = xThrow * Time.deltaTime * controlSpeed;
        float rawXPos = transform.localPosition.x + xOffset;
        float clampedXPos = Mathf.Clamp(rawXPos, -xRange, xRange);

        // Eixo Y
        yThrow = Input.GetAxis("Vertical");
        float yOffset = yThrow * Time.deltaTime * controlSpeed;
        float rawYPos = transform.localPosition.y + yOffset;
        float clampedYPos = Mathf.Clamp(rawYPos, -yRange, yRange);

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

Thanks

Hi Luis,

Happy New Year 2022, and welcome to our community! :slight_smile:

Thanks for describing your problem so well. That’s helpful. Three potential reasons for the problem are coming to my mind:

  1. There might be a collider which moves the spaceship.
  2. The range values might be wrong.
  3. You are getting input values because of a hardware issue. A couple of years ago, I got expected input values from my gamepad which was buried underneath a stack of paper.

Have you already tried to add Debug.Logs to your code to see what is going on during runtime?

thanks to the quick repply. so,

1- the ship has a few coliders yes, but nothing is touching then in the entire game. They also dont touch each other. But i tested this with all of them disabled, nothing changed
2- i got the code as in their GitHub. Debug it, dont see anything wrong
3- i’m using the laptop keyboard, and i really hope nothing is broken here. Tested with the code updated to the coursor keys, i still navigate the ship, but it’s the same issue.

Now i realized if the mouse is inside the game window, but outside the camera, that happens: It must be within the green rectangle. But i wish there was a better answer

  1. It’s good that you also tested the game with disabled colliders. Since the issue persisted, we now know that the problem is not caused by the colliders of the spaceship.
  2. The clamp values do not have anything to do with the code. Since your game is your game, it might be that it is slightly different from Rick’s. Try move the ship with the unclamped values to see if the issue persists.
  3. Log the input values into your console to see what’s going on during runtime. When you are not pressing the keys, the values should be 0. If they are not 0, there might be a problem with the hardware. If they are 0, the problem is somewhere else.

Privacy & Terms