Applying diffrent rotations on rigidbody

I’m new to Unity but worked a programmer for many years, mostly on the backend-side of game development, frontend and 3d programming is kind of new for me.

I’m building a hoverracer, however the rotation dynamics in Unity confuses me alot.
At this state I’m only applying torque based on the Horizontal input axis on my craft.

But there is other factors I want the hovercraft to rotate to:

  • Tilt: I want to tilt the craft when i steer it.
  • The ground: Since i use a ray to measure the hovercraft distance to the ground (to be able to add a scaleble downforce to get the craft hover) i can get the normal from the ground. How do i apply it’s rotation (without loosing the steering) to the craft.
  • I want the craft to slowly always level out, for instance when it jumps i want the craft level out to it’s “normal” state.

I really have no clue howto apply all this rotation on a object. Everything i try just breaks my steering.

Here is a snippet from my craft-skript (C#):

	/*
	 * Turn
	 */
	float rotation = Input.GetAxis("Horizontal") * rotationSpeed;
	rotation *= Time.deltaTime;
	transform.Rotate(0, rotation, 0);
	rigidbody.AddTorque(0,rotation,0);		
	/*
	 * Forward
	 */
	rigidbody.AddForce (transform.forward * (Input.GetAxis("Vertical")*100000));
	double vel = rigidbody.velocity.magnitude * 3.6;
	txtVelocity.text = "Speed: " + vel.ToString() + "km/h";
	txtGroundDistance.text = "Distance to ground: " + distanceToGround.ToString() + "m";
	txtGravity.text = "Hover force: " + gforce.ToString();

I looking for a similar bit of code; I’m working on a space shooter and want to allow collision with objects to knock the ship about tilting it off its usual axis however I want it to return to level slowly after such an impact.

I took a look at (this posting) but didn’t get quite the results I was after. Digging through a couple of other postings I came up with the following

Vector3 predictedUp = Quaternion.AngleAxis(
	            rigidbody.angularVelocity.magnitude * Mathf.Rad2Deg * stability / speed,
	            rigidbody.angularVelocity) * transform.up;
				
				Vector3 torqueVector = Vector3.Cross(predictedUp, new Vector3(0f,0f,-1f));
				//torqueVector = Vector3.Project(torqueVector, transform.up);
	        	rigidbody.AddTorque(torqueVector * speed * speed);

The above thanks to this posting