Hello there, hows it going?
Im going to be using Unity to create a 3D space flight game. I would like the controls to function like the controls from the game FreeLancer. Mainly the “Mouse Flight” function. So for i have almost managed to get it to work but for some reason when the model rotates up it will not rotated the full 360 degrees.
Any help on this would be greatly appreciated.
Thanks.
I used the LookAtMouse script and just deleated the sections for the PlayerPlane
var speed = 4.0;
function Update () {
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hitdist = 10.0;
var targetPoint = ray.GetPoint(hitdist);
var targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.deltaTime);
}
Ok, i almost have these controls working. The only problem now is the axis are swapped around.
Left = Down
Right = Up
Down = Right
Up = Left
Could anyone give me a hint on how to fix this?
var rSpeed = 1.0; // Scale. Speed of the movement
function FixedUpdate ()
{
MousePosition = Input.mousePosition;
MousePosition.y = (Screen.height/2) - Input.mousePosition.y;
MousePosition.x = (Screen.width/2) - Input.mousePosition.x;
transform.Rotate(MousePosition * Time.deltaTime * rSpeed, Space.Self);
}
Thanks.
Ok I got it to work.
var rSpeed = 1.0; // Scale. Speed of the movement
function FixedUpdate ()
{
MousePosition = Input.mousePosition;
MousePosition.x = (Screen.height/2) - Input.mousePosition.y;
MousePosition.y = -(Screen.width/2) + Input.mousePosition.x;
transform.Rotate(MousePosition * Time.deltaTime * rSpeed, Space.Self);
}
This is an old topic, but I wanted to thank you for this script. Mouselook/Plus/Plus 2 was getting inverted when I rotated too much. Being a non-coder, I was overcomplicating my attempts at a solution. This works brilliantly.
Now I have a simple thrust script on my camera, a kinematic rigidbody sphere called “Followtarget” parented to it at a distance, which serves as a makeshift reticule, and have the ship following the Followtarget with Smoothfollow 2. It’s working exactly as I wanted it to, the ship swoops around following the mouse as it flies. It’s not realistic, but it’s perfect for my project. 0 animation required, which is another area I’m still learning.
I’m posting this mainly to say thanks and to encourage people to post their simple scripts, even if they seem obvious… newbies like me need all the help we can get. And seeing how simple this script was taught me a bit.
I would like to agree with Thoth in saying thank you for this. I’m a decent coder, but this was one of the things that still eluded me. I’ve been a fan of the freelancer movement system for ages and your code was very helpful, keep up the good work! 
yeah…old topic but i was using this version, too.
maybe someone could need this basic freelancer style spaceship controls:
private var rotationMouseSpeed = 0.04;
private var forwSpeed = 0.18; // max normal speed
private var backSpeed = 0.04;
private var accSpeed = 0.0005;
private var inertiaFactor = 0.955;
private var curForwAcc:float = 0.0; // current acceleration FORWARD
private var curForwSpeed:float = 0.0; //
private var curBackAcc:float = 0.0; // current acceleration BACKWARD
private var curBackSpeed:float = 0.0; //
private var speedMaxFactor:float = 1.0;
private var mouseMovement:Vector3;
private var SystemStatus:boolean = true;
function Update(){
/////////////////////////////////
// toggles ship systems on/off
if(Input.GetKeyDown("escape")){
SystemStatus=!SystemStatus;
//toggles turretController script: player_turController
//BroadcastMessage ("TurretActivator");
}
//Debug.Log(SystemStatus);
////////////// ENGINES ONLINE ///////////////////
// all systems activated
if(SystemStatus){
// forward
if (Input.GetKey ("w") /* engineSound.audio.pitch <= engineSoundMaxPitch*/){
curForwAcc += accSpeed;
// engineSound.audio.pitch += enginePitchfactor;
// BroadcastMessage("JetLightsOn"); // jet lights controller - script: player_jets_light
StabilisingAgent();
}
// no movement? simulate inertia engine idle
//else if (engineSound.audio.pitch >= engineSoundMinPitch){
// engineSound.audio.pitch -= enginePitchfactor;
}
else{
curForwSpeed *= inertiaFactor;
// BroadcastMessage("JetLightsOff");// jet lights controller - script: player_jets_light
}
if (curForwSpeed <= 0.001){
curForwSpeed = 0.0;
}
// -----------------------------------------
// backward
if (Input.GetKey ("s")){
curBackAcc += accSpeed;
StabilisingAgent ();
}
else{ // no movement? simulate inertia
curBackSpeed *= inertiaFactor;
}
//BoosterSystemStatus
//if(player_EnerCal.BoostRechargeSwitch){ // if booster > 25% booster activated
if (Input.GetKey(KeyCode.LeftShift) Input.GetKey("w")){
if(speedMaxFactor <= 1.5){
speedMaxFactor += 0.03; // making it faster
}
// player_EnerCal.SendMessage("DischargeBoost");
}
else if(speedMaxFactor >= 1.0){
speedMaxFactor -= 0.03; // no speed factor, so to speak
}
//}
}
////////////// ENGINES OFFLINE ///////////////////
// all systems deactivated
if(!SystemStatus){
curForwSpeed *= inertiaFactor;
//BroadcastMessage("JetLightsOff"); // jet lights controller - script: player_jets_light
if (curForwSpeed <= 0.001){
curForwSpeed = 0.0;
}
}
//----------------------------
}
// FIXED UPDATE: Movement
function FixedUpdate(){
////////////// ENGINES ONLINE ///////////////////
//disable mouse controls and rotation
if(SystemStatus){
if (Input.GetKey ("d")){
transform.Rotate(0,0,- 2.0);
StabilisingAgent();
}
// -----------------------------------------
if (Input.GetKey ("a")) {
transform.Rotate(0,0,2.0);
StabilisingAgent();
}
// for rotation
mouseMovement = (Input.mousePosition - (Vector3(Screen.width, Screen.height, 0) / 2.0)) * 0.1;
if (mouseMovement.sqrMagnitude < 1.0){
mouseMovement = Vector3(0,0,0);
}
else{
mouseMovement -= mouseMovement.normalized * 2.0;
}
// rotation
transform.Rotate(Vector3(-mouseMovement.y, mouseMovement.x, 0) * rotationMouseSpeed);
}
////////////////////////////////////////////////////
// forward movement
var movementVector:Vector3 = Vector3.forward * curForwSpeed - Vector3.forward * curBackSpeed;
transform.Translate(movementVector);
curForwAcc *= inertiaFactor;
curBackAcc *= inertiaFactor;
// add speed to the speed!
curForwSpeed += curForwAcc;
curForwSpeed = Mathf.Max(0.0, Mathf.Min(forwSpeed*speedMaxFactor, curForwSpeed));
curBackSpeed += curBackAcc;
curBackSpeed = Mathf.Max(0.0, Mathf.Min(backSpeed*speedMaxFactor, curBackSpeed));
}
function StabilisingAgent(){
rigidbody.velocity = Vector3(0,0,0);
rigidbody.angularDrag = 1.2;
}
I find this script. this is very useful. very thank you