# Rotate to a certain position

Hello UA, excuse my bad English

My problem is this, I am modeling a spaceship in 2D, an achievement that can look where I want, giving orders with the arrow keys, through LookAt (), but I wish that when you change the sense of direction also rotates about 30 degrees in that change, and once you reach this point, return to its original position

Example, I’m going forward and I want when I press forward and upward (upper right diagonal) to rotate, for example 20 degrees to the left, as would happen would happen to a ship in space and then return to its original position only to look upper right diagonal. I pass what I have so far.

``````void Update()
{
horizontalDirection = Input.GetAxis("Horizontal");
verticalDirection = Input.GetAxis("Vertical");

desiredDirection = new Vector3(horizontalDirection, 0, verticalDirection);
currentDirection = Vector3.Lerp(currentDirection, desiredDirection, 0.2f);
transform.LookAt(transform.position + currentDirection);

if(CONDITION)
{
euler.z = Mathf.Lerp(euler.z, desiredDirection.x * 70.0f, 0.2f);
rot = Quaternion.Euler(euler);
transform.rotation = Quaternion.Lerp (transform.rotation, rot, sensitivity);
}
}
``````

With this script, an achievement that rotate, but I can not find a CONDITION for if so it stops when it reaches the desired position

In another script handling the movement of the ship, which goes well

Tilting the spaceship body to the side you’re turning isn’t an easy thing to do: the axis you’re turning around gets inclined too, and the spaceship goes to very weird directions. The solution is to create an empty object - let’s call it spaceship - and child the spaceship’s body to it. The spaceship objet turns around the world Y axis, while its body child object tilts around its local Z axis only. This script should be attached to the spaceship object, and it expects to find a child object called “body” in order to tilt it when the spaceship turns. Attach your spaceship model to the body object.

The ASWD control used here isn’t the best possible choice; the best way would be A and D turning anti-clockwise and clockwise, respectively, and perhaps W and S moving forward and backwards, but it would require alterations in your movement scripts, thus I didn’t modify it. It’s better to avoid diagonal controls (A + W, for instance) because the body may become indecise about which side to tilt.

``````using UnityEngine;
using System.Collections;

public class Move2D : MonoBehaviour {

public float turnSpeed = 2f;
public float tiltSpeed = 6f;
public float tiltAngle = 20f;

Vector3 currentDirection = Vector3.forward;
Vector3 desiredDirection = Vector3.forward;
float angle = 0f;
float dir = 1f;
Transform body;

void Start() {

body = transform.Find("body");
}

void Update() {

float dTime = Time.deltaTime;
float horizontalDirection = Input.GetAxis("Horizontal");
float verticalDirection = Input.GetAxis("Vertical");
desiredDirection = new Vector3(horizontalDirection, 0, verticalDirection).normalized;
if (desiredDirection.magnitude>0.2f){  // if some new direction defined
dir = (Vector3.Cross(desiredDirection, currentDirection).y>0f)? 1f : -1f;
angle += tiltSpeed*dTime;
currentDirection = Vector3.Slerp(currentDirection, desiredDirection, turnSpeed*dTime);
} else {
angle -= tiltSpeed*dTime;
}
angle = Mathf.Clamp(angle, 0f, 1f);
transform.LookAt(transform.position + currentDirection);
body.localEulerAngles = new Vector3(0, 0, tiltAngle*angle*dir);
}
}
``````

thanks! thank you very much! helped me a lot!