No matter how I set it in scene view the model jumps to a verical position and just stays there.
I’ve checked and re-done the animation, swtiched out the model in Player Rig Empty, checked my code against Rick’s and it’s fundamentally the same (see code below)
Looking online all I can see is things about the “Gimbal Lock” from the Euler but how have I caused that?
Hopefully it’s something simple that I’ve missed, so can someone take a look?
Thanks
using UnityEngine;
public class PlayerControl : MonoBehaviour
{
[SerializeField]float offsetSpeed = 8f;
[SerializeField] float xRange = 8f;
[SerializeField] float yRange = 8f;
[SerializeField] float positionPitchFactor = -2f;
[SerializeField] float positionYawFactor = -2f;
[SerializeField] float controlPitchFactor = -10f;
[SerializeField] float controlRollFactor = -20f;
float yThrow;
float xThrow;
void Update()
{
ProcessTranslation();
ProcessRotation();
}
private void ProcessRotation()
{
float pitchDueToPosition = transform.localPosition.y * positionPitchFactor;
float pitchDueToControlFlow = yThrow * controlPitchFactor;
float pitch = pitchDueToPosition + pitchDueToControlFlow;
float yaw = transform.localPosition.x * positionYawFactor;;
float roll = xThrow * controlRollFactor;
transform.localRotation = Quaternion.Euler(pitch,yaw,roll);
}
private void ProcessTranslation()
{
xThrow = Input.GetAxis("Horizontal");
yThrow = Input.GetAxis("Vertical");
float xOffset = xThrow * Time.deltaTime * offsetSpeed;
float rawXPos = transform.localPosition.x + xOffset;
float yOffset = yThrow * Time.deltaTime * offsetSpeed;
float rawYPos = transform.localPosition.y + yOffset;
float clampedXPos = Mathf.Clamp(rawXPos, -xRange, xRange);
float clampedYPos = Mathf.Clamp(rawYPos, -yRange, yRange);
transform.localPosition = new Vector3(clampedXPos, clampedYPos, transform.localPosition.z);
}
}