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.