Hi, guys, did ship control different way, I believe we don’t need another input throw in rotation because it is already counted in position and we are using new position to calculate rotation.
There is code and some pictures. Hope it makes sense. Cheers !
using UnityEngine;
[System.Serializable]
public struct AxisRange
{
public float min;
public float max;
public AxisRange(float min, float max)
{
this.min = min;
this.max = max;
}
}
[System.Serializable]
public struct PositionFactor
{
public float pitch;
public float yaw;
public float roll;
public PositionFactor(float pitch, float yaw, float roll)
{
this.pitch = pitch;
this.yaw = yaw;
this.roll = roll;
}
}
[System.Serializable]
public struct ControlsParameters
{
public float controlSpeed;
public AxisRange xRange;
public AxisRange yRange;
public PositionFactor positionFactor;
public ControlsParameters(float speed, AxisRange xRange, AxisRange yRange, PositionFactor factor)
{
this.controlSpeed = speed;
this.xRange = xRange;
this.yRange = yRange;
this.positionFactor = factor;
}
}
public class PlayerController : MonoBehaviour
{
[SerializeField] ControlsParameters controlParameters =
new ControlsParameters(1f,
new AxisRange(-1f, 1f),
new AxisRange(-2f, .6f),
new PositionFactor(-20f, 20f, 40f)
);
// Start is called before the first frame update
void Start()
{
}
float UpdateXAxis()
{
float xThrow = Input.GetAxis("Horizontal");
return Mathf.Clamp(
transform.localPosition.x + Time.deltaTime * xThrow * controlParameters.controlSpeed,
controlParameters.xRange.min,
controlParameters.xRange.max);
}
float UpdateYAxis()
{
float yThrow = Input.GetAxis("Vertical");
return Mathf.Clamp(
transform.localPosition.y + Time.deltaTime * yThrow * controlParameters.controlSpeed,
controlParameters.yRange.min,
controlParameters.yRange.max);
}
float UpdateZAxis()
{
return transform.localPosition.z;
}
float UpdatePitch()
{
return transform.localPosition.y * controlParameters.positionFactor.pitch;
}
float UpdateYaw()
{
return transform.localPosition.x * controlParameters.positionFactor.yaw;
}
float UpdateRoll()
{
return transform.localPosition.x * controlParameters.positionFactor.roll;
}
void ProcessTranslation()
{
transform.localPosition = new Vector3(UpdateXAxis(), UpdateYAxis(), UpdateZAxis());
}
void ProcessRotation()
{
transform.localRotation = Quaternion.Euler(UpdatePitch(), UpdateYaw(), UpdateRoll());
}
// Update is called once per frame
void Update()
{
ProcessTranslation();
ProcessRotation();
}
}



