Hello,
I’m working on a space sim at the moment, and my current controls are quite difficult to use, as I have different input pairs for Roll, Pitch and Yaw. Is there any way that I could use the mouse’s movement to alter the pitch and yaw, as Pitch is up/down, and Yaw is left/right. An example of this mechanic would be No Man’s Sky, and I was wondering if anyone could help me implement it, with possibly some Camera delay too. My central control script is below.
void Start()
{
rigidBody = GetComponent<Rigidbody>();
OriginalDrag = rigidBody.drag;
OriginalAngularDrag = rigidBody.angularDrag;
for(int i = 0; i < transform.childCount; i++)
{
foreach(var componentsInChild in transform.GetChild(i).GetComponentsInChildren<WheelCollider>())
{
componentsInChild.motorTorque = 0.18f;
}
}
}
public void Move(float rollInput, float pitchInput, float yawInput, float throttleInput, float verticalInput, bool airBrakes)
{
this.RollInput = rollInput;
this.PitchInput = pitchInput;
this.YawInput = yawInput;
this.ThrottleInput = throttleInput;
this.AirBrakes = airBrakes;
this.VerticalInput = verticalInput;
Throttle = Input.GetAxis("Throttle");
ClampInput();
CalculateRollAndPitchAngles();
//AutoLevel();
CalculateForwardSpeed();
ControlThrottle();
CalculateDrag();
CalculateLinearForces();
CalculateTorque();
if(Throttle < 0.1f)
{
Vector3 currentVelocity = rigidBody.velocity;
Vector3 newVelocity = currentVelocity * Time.deltaTime;
rigidBody.velocity = currentVelocity - newVelocity;
}
//TPS();
}
void ClampInput()
{
RollInput = Mathf.Clamp(RollInput, -1, 1);
PitchInput = Mathf.Clamp(PitchInput, -1, 1);
YawInput = Mathf.Clamp(YawInput, -1, 1);
ThrottleInput = Mathf.Clamp(ThrottleInput, 0, 1);
}
void CalculateRollAndPitchAngles()
{
Vector3 flatForward = transform.forward;
flatForward.y = 0;
if(flatForward.sqrMagnitude > 0)
{
flatForward.Normalize();
Vector3 localFlatForward = transform.InverseTransformDirection(flatForward);
PitchAngle = Mathf.Atan2(localFlatForward.y, localFlatForward.z);
Vector3 flatRight = Vector3.Cross(Vector3.up, flatForward);
Vector3 localFlatRight = transform.InverseTransformDirection(flatRight);
RollAngle = Mathf.Atan2(localFlatRight.y, localFlatRight.x);
}
}
void AutoLevel()
{
BankedTurnAmount = Mathf.Sin(RollAngle);
if(RollInput == 0f)
{
RollInput = RollAngle * AutoRollLevel;
}
if(PitchInput == 0f)
{
PitchInput = -PitchAngle * AutoPitchLevel;
PitchInput -= Mathf.Abs(BankedTurnAmount * BankedTurnAmount * AutoTurnPitch);
}
}
void CalculateForwardSpeed()
{
Vector3 localVelocity = transform.InverseTransformDirection(rigidBody.velocity);
ForwardSpeed = Mathf.Max(0, localVelocity.z * 10);
}
void ControlThrottle()
{
if (Immobilized)
{
ThrottleInput = -0.5f;
}
ThrottleInput = Mathf.Clamp01(Throttle + ThrottleInput * Time.deltaTime * ThrottleChangeSpeed);
EnginePower = Throttle * MaxEnginePower;
}
void CalculateDrag()
{
float extraDrag = rigidBody.velocity.magnitude * DragIncreaseFactor;
rigidBody.drag = (AirBrakes ? (OriginalDrag + extraDrag) * AirBreaksEffect : OriginalDrag + extraDrag);
rigidBody.angularDrag = OriginalAngularDrag * ForwardSpeed / 1000 + OriginalAngularDrag;
}
void CalculateLinearForces()
{
Vector3 forces = Vector3.zero;
forces += EnginePower * transform.forward;
forces += VerticalInput * 100 * transform.up;
rigidBody.AddForce(forces);
}
void CalculateTorque()
{
Vector3 torque = Vector3.zero;
torque += PitchInput * PitchEffect * transform.right;
torque += YawInput * 100 * transform.up;
torque += RollInput * RollEffect * transform.forward;
torque += BankedTurnAmount * BankedTurnEffect * transform.up;
rigidBody.AddTorque(torque * AeroFactor);
}
public void Immobilize()
{
Immobilized = true;
}
public void Reset()
{
Immobilized = false;
}
I’m pretty new to Unity and C#, and I’d really appreciate your help. I’ve tried to use the Camera section in the FirstPersonController asset script, but I can’t wire it all up.