Tilt with rotation.

Hello everybody,

Iโ€™m trying to make my character tilting and rotating at the same time. And I have a little problem with rotation. Tilt function works well (character turns back to the right position), but the problem is: character turns back with its rotation to the first position too. Like a plane with an autopilot :wink: Have you any sugestions to solve this problem? Thank you for help :slight_smile:

Js

	var speed = 1.0;
	var smooth = 2.0;
	var tiltAngle = 30.0;
	
	function Update () {
		
		transform.Translate(Vector3.forward * Time.deltaTime * speed);


		
		var tiltAroundZ = Input.GetAxis("Horizontal") * tiltAngle;
		var tiltAroundX = Input.GetAxis("Vertical") * tiltAngle;
		var tiltAroundY = Input.GetAxis("Horizontal") * tiltAngle;
		var target = Quaternion.Euler (tiltAroundX, -tiltAroundY, tiltAroundZ);
		
	
		
		transform.rotation = Quaternion.Slerp(transform.rotation, target,
		 										Time.deltaTime * smooth);
		
	}

Not sure if this is what you are going for since you donโ€™t provide a clear vision for the behavior. To rotate on the โ€˜yโ€™, you will need to add a variable that you increment and decrement from each frame:

#pragma strict

var speed = 1.0;
var smooth = 2.0;
var tiltAngle = 30.0;
var ySpeed = 2.0;
private var yRotation = 0;

function Update () {
	 transform.Translate(Vector3.forward * Time.deltaTime * speed);
	 
	 var tiltAroundZ = Input.GetAxis("Horizontal") * tiltAngle;
	 var tiltAroundX = Input.GetAxis("Vertical") * tiltAngle;
	 yRotation -= Input.GetAxis("Horizontal") * ySpeed;
	 var target = Quaternion.Euler (tiltAroundX, yRotation, tiltAroundZ);
	 
	 transform.rotation = Quaternion.Slerp(transform.rotation, target, Time.deltaTime * smooth);
}