keep player centred in grid based movement
What I am trying to do is have the player move from tile to tile like you would in pokemon etc only this movement will eventually be an infinite runner (for now its on getKey) and the player should only be allowed to turn when they reach the centre of a tile thus keeping the player contained to a grid based movement,
This could be tedious if they miss the centre of the tile so i want to set a boolean when the player presses left or right between x and y and perform the action when the player is in the middle of the tile.
I have followed a tutorial online to make a pokemon equivalent where i have a custom plane and a cube (player) with a script attached that essentially has a start point an endpoint and Lerps between the two and I am trying to code my extras on top by setting a boolean when the user can press left or right and checking the boolean when the user reaches the end of the lerp (not the centre of the tile which maybe my mistake) but this only really works when the speed is low when i increase the speed it starts turning seemingly whenever it feels like it, can anyone help me I'm still new to unity,
Here is what i have so far
public class player_movement : MonoBehaviour {
public Vector3 startPoint;
public Vector3 endPoint;
public float speed;
public float increment;
public bool isMoving;
public bool turnLeft;
public bool turnRight;
// Use this for initialization
void Start () {
startPoint = transform.position;
endPoint = transform.position;
}
// Update is called once per frame
void Update () {
if(increment <= 1 && isMoving == true)
{
increment += speed / 100;
}
else
{
isMoving = false;
}
if(increment >= 1 && turnLeft)
{
increment = 0;
isMoving = true;
startPoint = transform.position;
endPoint = new Vector3(transform.position.x, transform.position.y, transform.position.z);
transform.Rotate(0, -90, 0);
turnLeft = false;
turnRight = false;
}
if (increment >= 1 && turnRight)
{
increment = 0;
isMoving = true;
startPoint = transform.position;
endPoint = new Vector3(transform.position.x, transform.position.y, transform.position.z);
transform.Rotate(0, 90, 0);
turnRight = false;
turnLeft = false;
}
if (isMoving)
transform.position = Vector3.Lerp(startPoint, endPoint, increment);
if(Input.GetKey("w") && isMoving == false)
{
increment = 0;
isMoving = true;
startPoint = transform.position;
if(transform.eulerAngles.y == 0)
{
endPoint = new Vector3(transform.position.x, transform.position.y, transform.position.z + 1);
}
if (transform.eulerAngles.y == 90)
{
endPoint = new Vector3(transform.position.x + 1, transform.position.y, transform.position.z);
}
if (transform.eulerAngles.y == 180)
{
endPoint = new Vector3(transform.position.x, transform.position.y, transform.position.z - 1);
}
if (transform.eulerAngles.y == 270)
{
endPoint = new Vector3(transform.position.x - 1, transform.position.y, transform.position.z);
}
//endPoint = new Vector3(transform.position.x, transform.position.y, transform.position.z + 1);
}
else if (Input.GetKey("a") && increment > 0.5 && increment < 0.95)
{
turnLeft = true;
turnRight = false;
}
else if (Input.GetKey("d") && increment > 0.5 && increment < 0.95)
{
turnRight = true;
turnLeft = false;
}
}
}
ok ive changed my script to use moveTowards but still no joy heres the new script
using UnityEngine;
using System.Collections;
public class grid_movement : MonoBehaviour
{
public float speed = 2.0f;
public bool turnRight;
public bool turnLeft;
private Vector3 pos;
private Transform tr;
void Start()
{
//Here we set the Values from the current position
pos = transform.position;
tr = transform;
}
void Update()
{
Movement();
}
private void Movement()
{
//If we press any Key we will add a direction to the position ...
//using Vector3.'anydirection' will add 1 to that direction
//But we Check if we are at the new Position, before we can add some more
//it will prevent to move before you are at your next 'tile'
if (Input.GetKey(KeyCode.D))
{
//pos += Vector3.right;
turnRight = true;
turnLeft = false;
//tr.Rotate(0, 90, 0);
}
else if (Input.GetKey(KeyCode.A))
{
//pos += Vector3.left;
turnLeft = true;
turnRight = false;
//tr.Rotate(0, 90, 0);
}
else if (Input.GetKey(KeyCode.W))
{
//this works but i want it to move automatically
//pos += Vector3.forward;
}
else if (Input.GetKey(KeyCode.S))
{
//pos += Vector3.back;
}
//this moves automatically but wont allow me to turn
if (turnLeft && tr.position == pos)
{
tr.Rotate(0, 90, 0);
turnRight = false;
turnLeft = false;
}
else if (turnRight && tr.position == pos)
{
tr.Rotate(0, -90, 0);
turnRight = false;
turnLeft = false;
}
if(!turnRight && !turnLeft)
{
pos += Vector3.forward;
}
transform.position = Vector3.MoveTowards(transform.position, pos, Time.deltaTime * speed);
//Here you will move Towards the new position ...
}
}