Making a space sim

I’m making a space sim and have a planet (sphere) and a space ship model, as well as the 3rd person camera setup.

The problem I’m having is making it so the user can move forward, backwards, left and right, and use the mouse, as if really in space.

I’m really new to Unity so would just love some direction/help so I can better figure this all out.

Thanks! :slight_smile:

Hi, welcome to the forum!

You can use the Input.GetAxis function to read the mouse. The axis names are “Mouse X” and “Mouse Y” respectively. These give you the mouse delta (ie, the distance it has moved on the last frame), so you can use the mouse continuously to control the ship. Are you using physics to move the ship or handling the movement yourself with transform.Translate?

Well a friend gave me a movement script he wrote up in javascript but now, when I play the scene, the camera, which used to be behind the ship, now shows up in front, and is jittering back and forth.

Here’s the code for the PlayerMovement.js

var playerVehicle : Rigidbody;
var mainCamera : Rigidbody;
var mainCamTransform : Transform;

// Settings
var topSpeed = 0.0;
var superSpeed = 0.0;

var playerDrag = 0.0;

// Current Vehicle Speed
private var speed = 0.0;

// Mouse Speed
var sensitivityX = 0.0;
var sensitivityY = 0.0;
var senseFactor = 0.0; // For + / - sensitivity adjustment

var sensitivityRoll = 0.0;
var stopSpeed = 0.0;

private var pitch = 0.0;
private var yaw = 0.0;
private var roll = 0;

private var moveDirection = Vector3.zero;
private var turnRads = Vector3.zero;
private var turnScale = Vector3.zero;

static var stopState = "";

private var screenlock = true;

// Sway
var defaultCameraPosition = Vector3(0,100,-700);
var defaultRotationOffset = Quaternion.Euler(0, 0, 0);
var swayFactor = 100;
private var vectorScale = Vector3(1, 1, 1);

private var gameStarted = false;

function OnGUI () {
	gameStarted = true;
}

function Update () {
	// Sensitivity Adjustment
	if (Input.GetKeyDown (KeyCode.Equals)) {
		sensitivityX += senseFactor;
		sensitivityY += senseFactor;
		sensitivityRoll += senseFactor;
		print("Sensitivity: " + sensitivityX + ", " + sensitivityY);
	}
	if (Input.GetKeyDown (KeyCode.Minus)) {
		print ("minus");
		sensitivityX -= senseFactor;
		sensitivityY -= senseFactor;
		print("Sensitivity: " + sensitivityX + ", " + sensitivityY);
	}
	
	// Super Speed
	if (Input.GetKey(KeyCode.LeftShift)) {
		speed = superSpeed;
		stopState = "SUPAA Speeedoh~";
	}
	else {
		speed = topSpeed;
		stopState = "";
	}
	
	// print(mainCamTransform.rotation);
	// print(mainCamTransform.localPosition);
	
	// Always make sure the camera is fixed behind the player.
	mainCamTransform.localPosition = defaultCameraPosition;
	mainCamTransform.localRotation = Quaternion.identity;
}

function FixedUpdate() {	
	// Braking / Stopping
	if (Input.GetButton ("Stop")  playerVehicle.velocity.magnitude > 50) {
		playerVehicle.drag = 5;
		playerVehicle.angularDrag = 5;
		stopState = "Braking hard!";
	} else if (Input.GetButton ("Stop")) {
		playerVehicle.drag = playerDrag;
		// Rotation
		playerVehicle.angularDrag = 10;
		// HUD brake notification
		stopState = "Braking.";
	} else {
		playerVehicle.drag = 1;
		playerVehicle.angularDrag = 3;
		stopState = "";
	}
	
	/* Input */
	moveDirection = Vector3(Input.GetAxis("Horizontal"), Input.GetAxis ("Jump"), Input.GetAxis("Vertical"));
	// Mouse  Roll Rotation
	pitch = Input.GetAxis ("Mouse Y");
	yaw = Input.GetAxis ("Mouse X");
	roll = Input.GetAxis("Roll");
	
	turnRads = Vector3(pitch, yaw, roll);
	turnScale = Vector3(sensitivityY, sensitivityX, sensitivityRoll);
	
	if (gameStarted) {
		playerVehicle.AddRelativeTorque(Vector3.Scale(turnRads, turnScale));
	
		// Position Movement
		moveDirection *= speed;
		playerVehicle.AddRelativeForce(moveDirection);
	}
}

Any ideas on what I should look for/at?

I’m trying to use the mouse to let the user move the ship around and the up and down arrows to speed up and slow down.

Update…

I started over, created a sphere, light, camera (default) and capsule.

I’ve used the following script (MoveAround.js) and it works fine, if the capsule is on a plane of some sort… if I take the plane away, so he’s flying in “space”, he drops…

Any ideas on how to make it so he can move around without falling? I tried turning off gravity, but then he can’t move at all… :frowning:

oh sorry - the MoveAround.js is this:

var speed = 3.0;
var rotateSpeed = 3.0;

function Update ()
{
	var controller : CharacterController = GetComponent (CharacterController);
	
	transform.Rotate(0, Input.GetAxis("Horizontal") * rotateSpeed, 0);
	
	var forward = transform.TransformDirection(Vector3.forward);
	var curSpeed = speed * Input.GetAxis("Vertical");
	controller.SimpleMove(forward * curSpeed);
}

@script RequireComponent(CharacterController)