Hi there! I want to ask a simple question. I’m following this lecture quite slowly and I’ve reached the point of dealing with Yaw. However, I have this issue:
In the beginning, I have a PositionYawFactor of 5, which is used by almost everyone here and it has good results. However, it resulted in the not so good looking rotation at the beginning of the video (plus the ship starts looking at the wrong side). After I change it to -0.2 (after lots of tweaking), it seems to be working “better”, but it’s honestly doesn’t make much of a difference. If I leave it at 0, I’ll get almost the same result. Any ideas? Thanks for your time.
Code (similar to Ben’s)
[Tooltip("In ms^-1")] [SerializeField] float speed = 35f; // it's really m/s: meters ^ s-1 = meters/s
[Tooltip("In ms")] [SerializeField] float xRange = 25f; // how far the ship can go left or right (position is ALWAYS RELATIVE TO THE CAMERA!)
[Tooltip("In ms")] [SerializeField] float yRange = 12f; // how far the ship can go left or right (position is ALWAYS RELATIVE TO THE CAMERA!)
[SerializeField] float positionPitchFactor = -1.3f;
[SerializeField] float controlPitchFactor = -20f;
[SerializeField] float positionYawFactor = -0.2f;
[SerializeField] float controlRollFactor = -20f;
float xThrow, yThrow;
// Update is called once per frame
void Update()
{
ProcessTranslation();
ProcessRotation();
}
private 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);
}
private void ProcessTranslation()
{
xThrow = CrossPlatformInputManager.GetAxis("Horizontal"); // how far we're "Throwing" the stick, e.g. 1 is 100% to one side, -1 is -100% to the other side (assuming the stick is centered)
float xOffset = xThrow * speed * Time.deltaTime; // speed at THIS FRAME
yThrow = CrossPlatformInputManager.GetAxis("Vertical"); // how far we're "Throwing" the stick, e.g. 1 is 100% to one side, -1 is -100% to the other side (assuming the stick is centered)
float yOffset = yThrow * speed * Time.deltaTime; // speed at THIS FRAME
float rawNewXPos = transform.localPosition.x + xOffset;
float newXPos = Mathf.Clamp(rawNewXPos, -xRange, xRange);
float rawNewYPos = transform.localPosition.y + yOffset;
float newYPos = Mathf.Clamp(rawNewYPos, -yRange, yRange);
transform.localPosition = new Vector3(newXPos, newYPos, transform.localPosition.z);
}