First Person Smooth Movement Towards Camera
Hello, I am attempting to develop a first person controller with 3 inputs: a turns the camera 90 degrees left, d turns the camera 90 degrees right, and w will move the character 10 units in the direction it is facing (while ignoring key holding, each movement requires a new key down signal). The character can only move on the X/Z plane. I have successfully done what I needed with transforming the camera 10 units forward, but I would like better movement. I have been stuck for several hours attempting to figure out how to smoothly move the character. After looking at examples online, I have determined this code sample should do what I want, but the engine crashes on run.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class StepController : MonoBehaviour
{
// Lock Commands Unless Queue Is Empty
bool busy = false;
void Update()
{
// Direction Control
if (Input.GetKeyDown("a"))
{
transform.Rotate(0, -90, 0);
}
if (Input.GetKeyDown("d"))
{
transform.Rotate(0, 90, 0);
}
// Movement Control
if (Input.GetKeyDown("w"))
{
MoveForward(transform.position, new Vector3(transform.position.x + 10, transform.position.y, transform.position.z), 1.0F);
}
}
void Start()
{
Cursor.visible = false;
}
// Function For Moving In Cursor Direction
void MoveForward(Vector3 start, Vector3 end, float speed)
{
float startTime = Time.time;
float progress = 0F;
float moveLength = 10.0F;
float distCovered = 0F;
if (!busy)
{
busy = true;
while (progress < 1.0F)
{
distCovered = (Time.time - startTime) * speed;
progress = distCovered / moveLength;
transform.position = Vector3.Lerp(start, end, progress);
}
busy = false;
}
}
}
Does anyone have any idea how properly perform lerp in this situation? Also, is there an easy way to lerp a set distance according to current (in this case exclusively Z or X) looking direction?
Thanks in advance for the help! Beto