make my object face the direction of movement?

I have these two classes here, given to me as part of a project:

using UnityEngine;
using System.Collections;

public class Ball: MonoBehaviour {
  public float moveSpeed = 30;
  
  void Update () { 
		
	//var dir: Vector3 = Vector3.zero;
		
    CharacterController controller = GetComponent<CharacterController>();
		
    Vector3 moveDirection = Vector3.zero;

    moveDirection += Camera.main.transform.up * Input.GetAxis("Vertical");
    moveDirection += Camera.main.transform.right * Input.GetAxis("Horizontal");
    moveDirection.z = 0;
    
    controller.Move(moveDirection * moveSpeed * Time.deltaTime);
  }
}

and this one

using UnityEngine;
using System.Collections;

public class GamePad : MonoBehaviour {
	public float speed = 40;
	// Update is called once per frame
	void Update () {
		transform.Translate (Input.GetAxis("Horizontal")*Time.deltaTime*speed,
			Input.GetAxis ("Vertical")*Time.deltaTime*speed,
			0);
	}
}

these two I’m almost certain control movement along the X and Y axis. I have a sphere slightly streched and I want it to rotate it to the direction it is moving. Any ideas? Thanks!

In order to make your object face the direction you need to:

  • Create your stretch sphere on the Z axis (i.e. the head and tail need to be on the Z axis). If you are creating it from a standard sphere, just increase the size of the Z scale, or decrease the size of the x and y scale. In Unity, positive ‘Z’ is forward (which is where you want the the head).
  • Add the two lines of code at the bottom of this post at line 18 in the first script.

Note if you want a smoother rotation, take a look at the answer I posted for your other question (here)…the few lines posted by @runevision just after the bold ‘Edit’ are an implementation of a smoothed rotation that build on the two lines of code below.

 if (moveDirection != Vector3.zero)
   transform.rotation = Quaternion.LookRotation(moveDirection);