- Home /
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 ;) Have you any sugestions to solve this problem? Thank you for help :)
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);
}
Comment
Best Answer
Answer by robertbu · Nov 05, 2014 at 08:51 AM
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);
}