# Small rotation on axis while key pressed? (C#)

Hi.

I´m currently trying to learn Unity and C# with some hands on experience. I don’t know much code nor much about it’s proper structure.
I am trying to make an avoider game in unity where an object (player) can move from side to side. Got the basics down so far:

``````		if(Input.GetKey(KeyCode.A) && transform.position.z < 250)
{
transform.position = new Vector3 (0,transform.position.y,transform.position.z+speed);
}
``````

How should I proceed with adding an angle to my object while A is pressed? Im thinking an accleration on the z axis up to 20 degrees (much like a plane will angle while turning).
The rotation on the z-axis should reset as I let go of the key pressed.
(Player rotates up to 20 degrees while it is moving in specified direction and resetting afterwards)

Here is one way to do the rotation:

``````#pragma strict

var speed = 45.0;
var qTo : Quaternion;

function Start() {
qTo = Quaternion.identity;
}

function Update() {
if (Input.GetKeyDown(KeyCode.A))
qTo = Quaternion.Euler(0.0, 0.0, 20.0);
else if (Input.GetKeyUp(KeyCode.A))
qTo = Quaternion.identity;

transform.rotation = Quaternion.RotateTowards(transform.rotation, qTo, speed * Time.deltaTime);
}
``````

If you want a more eased movement, replace the ‘RotateTowards’ with ‘Slerp’ and reduce the speed to around 5.0.

I’m having trouble understanding you, but I make out you’re saying you want it so that you press A, and a plane tilts over to one side, and it cannot go past 20 degrees.

If I am right, this can be done by something like this.

(This is in Javascript, but I think C# is the same or at least very similar)

``````if (Input.GetKey(KeyCode.A)) {
transform.rotation.z += 0.5; //Change this to whatever you want.
if (transform.rotation.z > 20) {
transform.rotation.z = 20;
}
}
if (!(Input.GetKey(KeyCode.A)) {
transform.rotation.z = 0;
// or - if I misunderstood you
transform.rotation.z -= 0.5;
if (transform.rotation.z < 0){
transform.rotation.z = 0;
}
}
``````

I hope this helps you!