Quaternion and rotation of object

I am currently remaking a game that was a 2D college project, it is a 1945 clone. I have the movement controls sorted, now I am trying to rotate the player object left and right around 30 degrees, using a Quaternion.Lerp, so that it adds more of a feel to the horizontal movement.

I know a little C#, but apparently not enough as right now I am lost, I would really appreciate some guidance and some assistance. Thank you in advance!

here is the movement script, currently the only script in the game.

using UnityEngine;
using System.Collections;

public class MovementScript : MonoBehaviour {

	public float speed = 300f;
	public float xVelocity;
	public float borderX = 15;
	public float borderY = 10;

	void Update()
	{
		Movement();
	}

	void Movement() 
	{
		Vector3 pos = transform.position;
		Quaternion rot = transform.rotation;

		float moveHoriz = Input.GetAxis("Horizontal");
		float moveVert = Input.GetAxis("Vertical");

		xVelocity = rigidbody.velocity.x;

		float horizCalc = moveHoriz * speed * Time.deltaTime;
		float vertCalc = moveVert * speed * Time.deltaTime;

		float rotationAmount = 30;

		rigidbody.AddForce(horizCalc, 0, vertCalc);
		pos.x =  Mathf.Clamp(transform.position.x, -borderX, borderX);
		pos.z =  Mathf.Clamp(transform.position.z, -borderY, 0);
		transform.position = pos;

		transform.rotation = Quaternion.Lerp(Quaternion.Euler(Vector3(rot.x, rot.y, rot.z)), Quaternion.Euler(new Vector3(rot.x, rot.y,rot.z(rotationAmount * moveHoriz))), 1 * Time.deltaTime);

		if(pos.x <-borderX + 0.1 || pos.x > borderX - 0.1)
		{
			//xVelocity = 0f;
			if(moveHoriz <-0.1f)
				xVelocity = -1f;
			if(moveHoriz >0.1f)
				xVelocity = 1f;
		} 
	}
}

I’ll explain the most obvious problem with your current code, then list a few common ways you can get the rotation effect. I think you should try them all, and hopefully one will work for you or at least you’ll get a better understanding of things.

From your code:

Quaternion rot = transform.rotation;
...
Quaternion.Euler(Vector3(rot.x, rot.y, rot.z))

This is not right - the x, y, z properties of quaternions are not angles. What you want here is probably just “rot”, which is already a quaternion and can be passed straight in without decomposing to Euler angles and back.

For the other argument you wrote:

Quaternion.Euler(new Vector3(rot.x, rot.y,rot.z(rotationAmount * moveHoriz)))

maybe you wanted something like this:

Vector3 rotEuler = rot.eulerAngles;
Quaternion.Euler(new Vector3(rotEuler.x, rotEuler.y, rotEuler.z + rotationAmount * moveHoriz))

But, overall it is much simpler to just do something like this, instead of the lerp:

transform.rotation *= Quaternion.Euler(0, 0, rotationAmount * moveHoriz * Time.deltaTime);

This would normally make your ship rotate at a rate dependent on the horizontal input, without any inertia. You are using a rigidbody, though, so it’s not going to work well as the rigidbody will also be trying to drive transform.rotation. You might get better results in that case from:

rigidbody.angularVelocity = new Vector3(0, 0, rotationAmount * moveHoriz);

This sets the rigidbody’s angular velocity to whatever rotation speed you want, and lets the rigid body itself apply that back to the transfrom. It is still very crude.

The other thing to consider is whether you want to embrace the angular inertia that rigidbody is trying to provide for you, instead of stomping on it. So instead of directly modifying rotation or angularVelocity, you can apply torques to the rigidbody:

rigidbody.AddTorque(new Vector3(0, 0, rotationAmount * moveHoriz));

Adjust ‘rotationAmount’ and the rigidbody’s angular damping in the Inspector to tune the ramp-up rate, the maximum achieved rotation rate, and the ramp-down rate when you release the controls.