Rotate spaceship based on mouse movement

Hello guys,
First off, let me tell you I’ve really search A LOT, and I think I was always onto something, but never spot on…

So I have a spaceship in 3D space, similar to this pics bellow: (don’t judge, it’s paint)

I want my spaceship to kinda be stationary, meaning it shouldn’t really move much from that position, but I want to use my mouse to look around and ship’s rotation to follow let’s say that cursor in the center of the screen, or in other words - just to follow my camera’s rotation…

So if I were to move my mouse right, I guess this should happen:

Or if you ever happened to play StarConflict that’s spot-on what I’m trying to achieve… Here’s YouTube link to a video featuring that game and look at 10:43…

On another note I’m using rigidbody, and so far my Main Camera is outside my ship object, it has some script that just keeps it away from ship and keep it being there… I also managed to move my ship in the direction the camera is facing using this:

transform.rotation = Quaternion.FromToRotation(transform.up, GameObject.FindGameObjectWithTag("MainCamera").transform.forward) * transform.rotation;;

cacheRigidbody.velocity = GameObject.FindGameObjectWithTag("MainCamera").transform.forward * Input.GetAxis("Vertical") * 50;

Please help guys! :frowning:

Here is what I see in the video:

  • The camera follows ship. The ship does not follow the direction of the camera.
  • The camera has slerp/lerp rotations with limits that allow the ship to get ahead of the camera. This allows the camera to see part of the ship’s turn.
  • A ship has pitch, yaw, and roll (reference here). The camera is following (with lag) the pitch, yaw, and roll. There appears to some control (not the mouse cursor) that rolls the ship without turning the ship. There is also some roll (banking) associated with yaw turns.
  • Turns occur when when the cursor is outside the middle circle…above the circle, and you get a rotation on the ship’s local ‘x’ axis. To the side of the circle and ship rotates around the ‘y’ axis. For turns, the ‘z’ axis (banking) is driven by the ‘y’ rotation.
  • While I could not tell form the video, I’ll bet the speed of the turn depends on the distance the cursor outside the center circle.

Here is something really simple to get you started. It uses as square. I cannot tell if the ship in the video uses a square or a circle. This code does not deal with roll (banking), but just yaw and pitch. I uses a fraction of the screen height as the calculation so that movements on different resolution devices will be consistent:

#pragma strict

var noTurn = 0.1; // Extent of the no-turn zone as a fraction of Screen.height;
var factor = 150.0;
private var center : Vector3;

 function Start() {
 	center = new Vector3(Screen.width / 2, Screen.height / 2, 0);
function Update () {
	var delta = (Input.mousePosition - center) / Screen.height;
	if (delta.y > noTurn) 
		transform.Rotate(-(delta.y - noTurn) * Time.deltaTime * factor, 0, 0);
	if (delta.y < -noTurn)
		transform.Rotate(-(delta.y + noTurn) * Time.deltaTime * factor, 0, 0);
	if (delta.x > noTurn)
		transform.Rotate(0, (delta.x - noTurn) * Time.deltaTime * factor, 0);
	if (delta.x < -noTurn)
		transform.Rotate(0, (delta.x + noTurn) * Time.deltaTime * factor, 0); 

Works all right! Really thanks bro… I still am trying to alter this script to tilt the ship to the direction the ship is moving in.