Rotating spaceship controls

Just trying to learn some basic scripting, but I’m getting stuck on something that should be fairly simple.

Basically what I want is to be able to hold down the right mouse, and rotate the ships pitch, and yaw (so angle up / down, and turn left or right) and then take over with WASD from there, using the ships new local coords. All the while with the camera following and updating to the new ship coords

I’ve been trying to start off with a few scripts from here (the WoW camera script helped a lot), but I still can’t seem to get things working right. The best I can currently get is the yaw working, but anything else just spins the ship uncontrollably.

Any help would be greatly appreciated!

What have you got so far? Can we see some code?

I am currently experimenting with a similar starting point. The thing I have come to realise in doing so is that even in something so simple there are lots of ways of doing it!

Some points to clarify/consider:

  • Are you using rigidbodies, or moving the GameObject by translating?

  • How does your camera follow the ship? Is it parented, or does it have code to match position and translate backward each frame?

  • I am guessing this is in 3D space but, is it intended to be “true space” 3D or “ground-thinking” 3D? (ie. with an arbitary “down” direction and self righting mechanisms)

I’ll post code when I get back tonight, but I can provide a few details:

The ship (gameobject) is currently being rotated By the camera right click movement. Similar to WoW’s system when you right click and drag. The character continuously turns to match the new direction of the camera.

It’s true space right now - so there really isn’t a standard up or down.

I had an old control scheme that would use W and S to pitch up or down, and you could adjust with the mouse, but it felt too clumsy and not the best scheme scenarios like battles.

I’ll post some code tonight. As I said, I’ve just learning, so it’s mostly scripts that I’ve attempted to beat into submission :stuck_out_tongue:

I spent days messing with Quaternions for this because I used the basic FPS Cam as a starting point. However, I realised that I was overcomplicating things and all I really needed to do was this:

// get input axis
rotationX += Input.GetAxis("Mouse X") * sensitivityX;
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;

// move object based on these movements
transform.Rotate (-rotationY, rotationX, 0);
	
// erode values
rotationX = rotationX * 0.9;
rotationY = rotationY * 0.9;

// forward movement
shipVelocity = Input.GetAxis("Vertical") * 100;
transform.Translate(0, 0, -(shipVelocity*Time.deltaTime));

I’ve removed most of my checks value capping for simplicity but you can get the gist of it. I also expand on this by adding A D to bank, but the method is pretty much the same.

Incidentally, Quaternions came in handy later to expand on it. By Slerping between the ship’s rotation Quaternion and the camera’s rotation Quaternion, the camera swings around with a slight delay and making for a much smoother feel.

1 Like

Apologies for the late reply - and thanks for your input, Novodantis!

Here’s what I have currently. I’m no scripter, so the code is kind of hacked together from various sources. I’ll get better. I hope :stuck_out_tongue:

This is currently attached to my space ship. I have a slightly modified version of the WoWCamera attached to my Main Camera.

var rotateSpeed:float = 150.0;
var speed = 20.0;


function Update ()
{

var rotation = Input.GetAxis ("Horizontal") * rotateSpeed;

var vTranslation = Input.GetAxis ("Vertical") * speed;
var hTranslation = Input.GetAxis ("Horizontal") * speed;


vTranslation *= Time.deltaTime;
hTranslation *= Time.deltaTime;
rotation *= Time.deltaTime;


// Allow turning at anytime. Keep the character facing in the same direction as the Camera if the right mouse button is down.
	if(Input.GetMouseButton(1)) 
	{
		transform.rotation = Quaternion.Euler(Camera.main.transform.eulerAngles.x, Camera.main.transform.eulerAngles.y,0);
		
	}

/*
Move our player along it's new vertical axis
*/

transform.Translate (0, 0, vTranslation);
transform.Rotate (0, rotation, 0);


/*
This locks the cursor - making it invisible to the user.
*/

   if(Input.GetMouseButton(1) || Input.GetMouseButton(0))
      Screen.lockCursor = true;
   else
      Screen.lockCursor = false;

}

@script RequireComponent(CharacterController)

It works…I guess. Basically right click and drag, allows you to change the pitch and yaw of the spaceship, and then accelerate from that local direction. However, A&D do nothing in this mode (I’d expect them to rotate the ship). I’ve got strafing working in this mode (right click), but a strafing space ship just looks…odd.

Left click is a typical orbit. Using WASD allows you to move forward and back, and turn the ship left and right. This view also, does not always align the camera to the butt end of the spaceship every update. The only thing Left click won’t do, is give you any pitch adjustment controls. But it’s great for working on a horizontal plane :slight_smile:

If I could figure out how to smoothly integrate pitch changes into the left click mode, without spinning my ship uncontrollably, that would be perfect. Otherwise, I’d settle for turning in the right click mode.

I notice the jittering of the space ship when I use the follow cam (right click) as I’m constantly updating the camera to the ships rotation. I think you touched on this with your mention of Slerping. Thats something I’d love to fix.

No worries, adapting scripts is the way to go, especially to begin with!

I also had the jerking motion, and the reason I got it was that I wasn’t moving my camera in a LateUpdate() function. I suspect something similar is happening here. I’m guessing the WoW style camera moves on mouse inputs, and the ship is being turned to match the camera, rather than the other way around.

I think I can see the kind of control system you’re going for here, but I think the primary issue at hand is control assignment. You need at least two axes of rotation and speed control. Two alternatives I would suggest:

  • If you want to keep the rotation controlled by keys, use WSAD for direction and have two other keys (eg Z/X) to throttle up/down. You can use a toggle to switch horizontal between bank/yaw.

  • If you like mouse controlled fluency (I know I do), ditch rotating with keys and use them for forward/backward and bank. Have it so that a mouse button toggles between orbiting the camera around the ship, and controlling its direction.

As those two are quite vague and quick solutions, don’t hesitate to ask me if you’re unsure about doing either one!

Also, I wouldn’t use a character controller, as I believe their z rotation is frozen. They’re more for grounded objects. Instead I would use a sphere or capsule collider. And if you want to use rigidbodies for physics, you’ll need to change your Translates and Rotations for AddForce and AddTorques. Which is what I’m now working towards with my own one =)

Awesome advice here - thank you very much!

I’ll try the LateUpdate function to smooth things out.

In regards to controls, I want to keep them as simple and standard as possible. Basically Left Click will be Orbit, Right Click will be move mode - but with W acting as accelerate and S acting as brake. I’ll try and get A&D working as banking - but last time I tried that, it fought with the rotations of the camera, and caused the ship to jitter back and forth every tick.

I never thought to change over from the character controller. I’ve been using the physics system a bit, in some planetary orbit asteroid scripts, so I’ll see about porting the ship over to a physics based system. Maybe that in itself will iron out some of the kinks (or maybe it’ll just introduce a whole new series of bugs :stuck_out_tongue: )

Thanks again for the advice!