RE-EDIT: Here’s a video of the kind of gameplay I’m talking about, since I suck at explaining it.
EDIT: Got a few things figure’t. Basically, I’m making the character move based on the direction the camera is moving. With the old script, this meant that if the camera looked from above, the character would attempt to move down into the terrain. It was based on a rigidbody-based FPS walker.
I took a section from Lerpz’ ThirdPersonControl script, and added it in.
With my new mashup of two scripts, it now goes in the proper direction, but the acceleration system of the old script isn’t working.
Old script: Acceleration working, direction not working.
function FixedUpdate ()
{
if (grounded)
{
// Calculate how fast we should be moving
var targetVelocity = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
targetVelocity = camera.main.transform.TransformDirection(targetVelocity);
targetVelocity *= speed;
// Apply a force that attempts to reach our target velocity
var velocity = rigidbody.velocity;
var velocityChange = (targetVelocity - velocity);
velocityChange.x = Mathf.Clamp(velocityChange.x, -maxVelocityChange, maxVelocityChange);
velocityChange.z = Mathf.Clamp(velocityChange.z, -maxVelocityChange, maxVelocityChange);
velocityChange.y = 0;
rigidbody.AddForce(velocityChange, ForceMode.VelocityChange);
}
New script: Direction working derived from camera, but the forward variable somehow needs to match the character’s orientation. Acceleration not working. (Speed is absolutely constant.)
function FixedUpdate ()
{
if (grounded)
{
// Forward vector relative to the camera along the x-z plane
var forward = Camera.main.transform.TransformDirection(Vector3.forward);
forward.y = 0;
forward = forward.normalized;
// Right vector relative to the camera
// Always orthogonal to the forward vector
var right = Vector3(forward.z, 0, -forward.x);
// Calculate how fast we should be moving
var v = Input.GetAxisRaw("Vertical");
var h = Input.GetAxisRaw("Horizontal");
var targetVelocity = h * right + v * forward;
targetVelocity = targetVelocity.normalized;
targetVelocity *= speed;
// Apply a force that attempts to reach our target velocity
var velocity = rigidbody.velocity;
var velocityChange = (targetVelocity - velocity);
velocityChange.x = Mathf.Clamp(velocityChange.x, -maxVelocityChange, maxVelocityChange);
velocityChange.z = Mathf.Clamp(velocityChange.z, -maxVelocityChange, maxVelocityChange);
velocityChange.y = 0;
rigidbody.AddForce(velocityChange, ForceMode.VelocityChange);
}
[/code]