# Rotate Character on Sphere

Hey guys I am currently using the following script for my movement around a sphere;

This works great however I wanted to change the controls a bit, so currently I can move my ship around using WASD which works fine. However my ship is not rotating.

What I want to have is that my ship is rotating into the direction the player is moving, using the rotation available in this script will make the character endlessly rotating however I want that it stops as soon as he faces the direction it moves in.

Any clues how this could be achieved on a sphere with the current script ?

Here is my current script:

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

public class OrbitMovement : MonoBehaviour
{
GameObject orbit;
public float moveSpeed;
public float rotateSpeed;

public float angleX = 0;
public float angleY = 0;

public float angle = 0.0f;
public Vector3 direction = Vector3.one;
public Quaternion rotation = Quaternion.identity;

public virtual void Start()
{
//Get Orbit GameObject
orbit = GameObject.FindGameObjectWithTag("Orbit");

//If gameObject hast Box or Sphere Collider add its size to the moving radius
if (gameObject.GetComponent<BoxCollider>())

//Define final radius for objects to move on
}

// Update is called once per frame
public virtual void Update()
{
//Set direction
direction = new Vector3(Mathf.Sin(angle), Mathf.Cos(angle));

//Trigger Update Position
UpdatePositionRotation();

//Controls
Vector2 input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));

Vector2 moveDirection = new Vector2(input.x, input.y);
moveDirection *= moveSpeed;
Translate(moveDirection);

}

//Method for updating the objects position
void UpdatePositionRotation()
{
transform.localPosition = rotation * Vector3.forward * radius;
transform.rotation = rotation * Quaternion.LookRotation(direction, Vector3.forward);
}

public void Translate(Vector2 moveDirection)
{
Translate(moveDirection.x, moveDirection.y);
}

public void Translate(float x, float y)
{
Vector3 perpendicular = new Vector3(-direction.y, direction.x);
Quaternion verticalRotation = Quaternion.AngleAxis(y * Time.deltaTime, perpendicular);
Quaternion horizontalRotation = Quaternion.AngleAxis(x * Time.deltaTime, direction);
rotation *= horizontalRotation * verticalRotation;
}

//Set Spawncoords of Object at predefined location
public void SpawnLocation(float x, float y)
{
//Update direction
direction = new Vector3(Mathf.Sin(angle), Mathf.Cos(angle));

Vector3 perpendicular = new Vector3(-direction.y, direction.x);
Quaternion verticalRotation = Quaternion.AngleAxis(y, perpendicular);
Quaternion horizontalRotation = Quaternion.AngleAxis(x, direction);
rotation *= horizontalRotation * verticalRotation;
}

}
``````