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
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
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