I am trying to code up a simple first person grid movement system. The plan is for it to only receive input for movement if the player is aligned to the grid (I do this by checking via mod to see if there is no remainder).
That part works. But the movement, I can’t seem to get to work… And I know it is because I have done the movement wrong.
Here is my script
var gridSize : int = 4;
var moveSpeed : float = 4.0;
var canMove : boolean = false;
function Update()
{
var currentRotation = transform.rotation.y; //get current rotation along y axis
var currentX = transform.position.x; //get current x position
var currentZ = transform.position.z; //get current z position
//check if aligned to grid and not in middle of a rotation.
if (currentX % gridSize == 0 currentZ % gridSize == 0 currentRotation % 90 == 0) canMove = true;
else canMove = false;
//Debug.Log ("Player is aligned : " + canMove);
if (canMove)
{
if (Input.GetAxis("Vertical") > 0)
{
Debug.Log("Moving Forward");
MoveForward();
}
else if (Input.GetAxis("Horizontal") > 0)
{
Debug.Log("Turning Right");
TurnRight();
}
else if (Input.GetAxis("Horizontal") < 0)
{
Debug.Log("Turning Left");
TurnLeft();
}
}
}
function MoveForward()
{
var originalPosition = transform.position.x;
var newPosition = transform.position.x + 4.0;
transform.position.x = Mathf.Lerp(originalPosition, newPosition, Time.deltaTime * moveSpeed);
}
function TurnRight()
{
transform.rotation.y = Mathf.Lerp(transform.rotation.y, transform.rotation.y + 90, Time.time);
}
function TurnLeft()
{
transform.rotation.y = Mathf.Lerp(transform.rotation.y, transform.rotation.y - 90, Time.time);
}
What is wrong with the MoveForward, TurnRight and TurnLeft - the scriptingreference doesn’t seem to give much advice on Lerps.
Thanks!