How to have a human character tilt and rotate towards their movement?

Hello to all that read this!

I’m first and foremost a programmer, not an animator. I’m currently writing an in-depth procedural animation application, but for some god awful reason I can’t seem to both tilt my character in the direction he’s moving and rotate him towards his trajectory.

Here’s the code I have right now:

using UnityEngine;
using System.Collections;

public class Movement : MonoBehaviour 
{
	public float moveSpeed;			//How fast the character moves
	public Rigidbody controller;	//Rigidbody to move
	int gravity = 20;				//Gravity multiplier
	float verticalVel = 2f;			//Fall Velocity
	float smooth = 10f;				//Turn smoothing
	float tiltAngle = 3f;			//The angle at which to tilt the character

	Quaternion characterRotation;	//Stores the characters rotation

	public float horizontal;
	public float vertical;

	// Update is called once per frame
	void FixedUpdate () 
	{
		//Receives user input
		horizontal = Input.GetAxis ("Horizontal");
		vertical = Input.GetAxis ("Vertical");

		//Controls change based on camera position (This is for debugging purposes)
		Vector3 forward = Camera.main.transform.TransformDirection (Vector3.forward);
		forward.y = 0;
		forward = forward.normalized;

		Vector3 right = new Vector3 (forward.z, 0, -forward.x);

		//The direction the player is moving
		Vector3 inputVec = horizontal * right + vertical * forward;
		inputVec *= moveSpeed;

		//Move the controller in that direction
		controller.MovePosition (transform.position + (inputVec + Vector3.up * -gravity + new Vector3(0, verticalVel, 0)) * Time.deltaTime);

		//Calculates the tilt based on user input
		Quaternion target = Quaternion.Euler (vertical * tiltAngle, 0, -horizontal * tiltAngle);

		if (inputVec != Vector3.zero) 
		{
			//Smoothing rotates the character and assigns the new rotation to CharacterRotation
			characterRotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation (inputVec, Vector3.up), Time.deltaTime * smooth);
			//Merges both rotations and assigns it to the transforms'
			transform.rotation = characterRotation * target;
		}
	}
}

So, what happens here now is that the character tilts in the same direction no matter which way he moves. If I tell him to run towards the camera he tilts away from it, if I tell him to run away from the camera he tilts away from the camera, if I tell him to go left or right he again, tilts away from the camera.

My camera is completely stationary. It doesn’t follow the character or rotate towards the character. There are no user made scripts attached to the camera.

If anyone could help me that would be fantastic! Thank you to anyone that takes the time to read this question, any help would be appreciated! Also, if my explanation is vague, please tell me! I don’t really know the best way to communicate this issue.

My character is a human object.

So, this turned out to be a very simple mistake. - Like. Wow. So simple!

In the code posted in the question above I have this: (I’ve copied more than required in an effort to make it easier to find)

         //Move the controller in that direction
         controller.MovePosition (transform.position + (inputVec + Vector3.up * -gravity + new Vector3(0, verticalVel, 0)) * Time.deltaTime);
 
         //Calculates the tilt based on user input
       >>Quaternion target = Quaternion.Euler (vertical * tiltAngle, 0, -horizontal * tiltAngle);
 
         if (inputVec != Vector3.zero)

However, I never realised how silly it was to put the horizontal tilt in the z-axis float. Furthermore, I want absolute values so that the character will change rotation based on which direction he’s going and not continually tilt in the same direction regardless of vertical value. (+/-)

Here is the new code that has solved my issue and seems to work fairly well:

        //Calculates the tilt based on user input
      >>Quaternion target = Quaternion.Euler ((Mathf.Abs(vertical) + Mathf.Abs(horizontal)) * tiltAngle, 0, 0);

		if (inputVec != Vector3.zero) 

Note how both tilt calculations are in the same part of the Quaternion.Euler. This means that no matter which direction my character faces he will always tilt in the direction of his forward vector!

Also, in order to solve the problem of doubling up on my tilt, I added in a simple if statement that could probably be made better and more efficient, but for now this is what it looks like in the fully finished piece of code:

using UnityEngine;
using System.Collections;

public class Movement : MonoBehaviour 
{
	public float moveSpeed;		 //How fast the character moves
	public Rigidbody controller;	//Rigidbody to move
	float smooth = 10f;	         //Turn smoothing
	float tiltAngle;			    //The angle at which to tilt the character
	float offset;

	Quaternion characterRotation;	//Stores the characters rotation

	public float horizontal;
	public float vertical;

	// Update is called once per frame
	void FixedUpdate () 
	{
		//Receives user input
		horizontal = Input.GetAxis ("Horizontal");
		vertical = Input.GetAxis ("Vertical");

		//Controls change based on camera position (This is for debugging purposes)
		Vector3 forward = Camera.main.transform.TransformDirection (Vector3.forward);
		forward.y = 0;
		forward = forward.normalized;

		Vector3 right = new Vector3 (forward.z, 0, -forward.x);

		//The direction the player is moving
		Vector3 inputVec = horizontal * right + vertical * forward;
		inputVec *= moveSpeed;

		//Move the controller in that direction
		controller.MovePosition (transform.position + (inputVec) * Time.deltaTime);

		//Determines the level of tilt based on user input
		if (Mathf.Abs (vertical) > 0 && Mathf.Abs (horizontal) > 0) 
		{
			tiltAngle = 1.5f;
		} 
		else 
		{
			tiltAngle = 3f;
		}

		//Calculates the tilt based on user input
		Quaternion target = Quaternion.Euler ((Mathf.Abs(vertical) + Mathf.Abs(horizontal)) * tiltAngle, 0, 0);

		if (inputVec != Vector3.zero) 
		{
			//Smoothing rotates the character and assigns the new rotation to CharacterRotation
			characterRotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation (inputVec, Vector3.up), Time.deltaTime * smooth);
			transform.rotation = characterRotation * target;
		}
	}
}

I hope this helps other people!