- Home /
How to rotate a spacecraft using the mouse instead of butttons?
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.
I can't help directly, because i've never programmed anything like this myself, but as a gamer i'd like to make a personal remark: I think the flight controls in No $$anonymous$$an's Sky were "sub-optimal". Check out "Galaxy on Fire", i think the controls were pretty much perfect when played with mouse + keyboard.
Thanks for your help. Yeah, N$$anonymous$$S was a pretty clunky and unresponsive game when it came to spacecraft controls. I will go and check out Galaxy on Fire.
Answer by Creeper_Math · Jan 18, 2017 at 07:04 PM
Use the Input.GetAxis(string AxisName)
for this... Here's the ones that would work here
float YawChange = Input.GetAxis("Mouse X");
float PitchChange = Input.GetAxis("Mouse Y");
//("Yaw" and "Pitch" are public variables)
Yaw += YawChange;
Pitch += PitchChange;
Note that the input.getaxis gets the movement of the mouse, so you will have to use them as adding and subtracting like above
Thanks, bro! You made it look so simple... the FPS controller used $$anonymous$$athf.Epsilon and other $$anonymous$$athf stuff that I don't even know how to use! I will test it out when I get the chance.
Do you have any idea how i would delay camera rotation, so it was slightly slower to rotate into place?
Here's the same script with the customized "sensitivity" (set to decimals for lower sensitivity)
float YawChange = Input.GetAxis("$$anonymous$$ouse X");
float $$anonymous$$chChange = Input.GetAxis("$$anonymous$$ouse Y");
float YawSensitivity = 1.0f;
float $$anonymous$$chSensitivity = 1.0f;
//("Yaw" and "$$anonymous$$ch" are public variables)
Yaw += YawChange * YawSensitivity;
$$anonymous$$ch += $$anonymous$$chChange * $$anonymous$$chSensitivity;
Your answer
![](https://koobas.hobune.stream/wayback/20220612104813im_/https://answers.unity.com/themes/thub/images/avi.jpg)