Restrict rotation to 2d space

I’m making a mod out of a sample project posted a few years ago.

I plan on using this as a basic game engine for a game i have in my mind and will be issuing credits to the creator, of course. :smiley:

So, I want the controls to behave more like an asteroid clone.
I’ve added a script for it to use the position of the mouse to determine rotation ‘direction’. But it seems to be throwing it out of whack. When I test it, the ship seems to point down. How can I restrict the gameobject to only ‘2d space’? Thanks!

#pragma strict
#pragma implicit
#pragma downcast

var thrustSpeed : float = 40;	// How fast we accelerate
//var rotateSpeed : float = 300;	// How fast we turn
var horizontalSpeed : float	 = 2.0; //rot speed for mouse
var verticalSpeed : float = 2.0;  //rot speed for mouse
var maxVelocity : float = 150;	// Square magnitude value of highest speed
var bulletSpeed : float = 100;	// How fast bullets go
var respawnDelay : float = 2.5;	// Seconds after dying before new ship spawns
var bullet : GameObject;		// Bullet prefab
var explosion : GameObject;		// Explosion prefab
private var myTransform : Transform;
private var myRigidbody : Rigidbody;
private var blowingUp = false;

function Start() {
	// Cache "hidden" GetComponent calls for extra speed
	myTransform = transform;
	myRigidbody = rigidbody;
	Manager.startPosition = transform.position;
	// Associate cameras with ship, so the screen follows the ship as it moves around
	var cameras = GameObject.FindGameObjectsWithTag("MainCamera");
	for (cam in cameras) {
		var camScript : FollowObject = cam.GetComponent(FollowObject);
		camScript.objectToFollow = transform;

function LateUpdate() {
	if (Input.GetButtonDown("Fire1")) {Shoot();}

function FixedUpdate() {
	// Make sure ship's rigidbody never sleeps, so moving triggers will always be affected
	if (myRigidbody.IsSleeping()) {myRigidbody.WakeUp();}
	// Rotate ship
	//myTransform.Rotate(Vector3.up*(Input.GetAxis("Horizontal") * rotateSpeed * Time.deltaTime) );
	var h : float = horizontalSpeed * Input.GetAxis("Mouse X");
	var v : float = verticalSpeed * Input.GetAxis("Mouse Y");
	transform.Rotate (v, h, 0);
	// Move ship forward
	if (Input.GetButton("Thrust") ) {
		audio.volume = 1;
		if (myRigidbody.velocity.sqrMagnitude < maxVelocity) {
			// Add less force as we approach top speed
			myRigidbody.AddRelativeForce(Vector3.forward * (thrustSpeed - myRigidbody.velocity.magnitude) );
	else {audio.volume = 0;}

function Shoot() {
	var newBullet : GameObject = Instantiate(bullet, myTransform.position + myTransform.forward*1.7, myTransform.rotation);
	newBullet.rigidbody.velocity = myRigidbody.velocity;	// Start bullet with ship's velocity
	newBullet.rigidbody.AddRelativeForce(Vector3.forward * bulletSpeed);
	Physics.IgnoreCollision(newBullet.collider, collider);	// Don't hit ourselves with bullet

function OnCollisionEnter() {
	if (blowingUp) {return;}	// Make sure the Explode function is only called once
	blowingUp = true;
	// Since an object being destroyed doesn't seem to make OnTriggerExit fire, do it manually for all triggers
	var triggers = GameObject.FindGameObjectsWithTag("trigger");
	for (t in triggers) {
		t.SendMessage("OnTriggerExit", collider, SendMessageOptions.DontRequireReceiver);
	// Blow up ship and respawn after a certain time
	Instantiate(explosion, transform.position, Quaternion.identity);

A simple way to do it would be to add a RigidBody to it and use “Freeze Rotation” on the axis you don’t want to rotate. You could also use script to always keep one axis at 0 or 90 or whatever it needs to be to point the right way.

function Update()
    transform.rotation.x = 0;

this would probably take up some of the processor space though, not sure.