- Home /
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! :(
Answer by robertbu · Jul 14, 2014 at 06:27 AM
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;
Debug.Log(delta);
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);
}
Great answer! It's a bit strange that I'm answering this almost a year later, but my passion for this hasn't passed away. I'll be playing with this in following weeks and will write here my progress/additional questions..
Once again, thank you, will mark it as answer as soon as I get it in-editor. :)
Answer by SyndicateXx · May 30, 2015 at 11:30 AM
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.
Your answer
Follow this Question
Related Questions
rotate camera on mouse reaching edges 1 Answer
Make the player go into the direction I am looking at 0 Answers
Camera Shaking At High Speed 1 Answer
Adjust camera rotation while looking at a target 1 Answer
Relative Movement Problem 1 Answer