- Home /
Having trouble with grid movement
So I am trying to get grid movement to work properly I am aiming for movement kind of like crossy roads. I have this code so far but if the player presses their movement to fast it goes off of the grid so instead of moving by 1 in x direction it will move by some decimal number. Any help would be appreciated.
using UnityEngine;
using System.Collections;
public class NewPlayer : MonoBehaviour {
float lerpTime;
float currentLerpTime;
float perc = 1;
public static Transform playerTransform;
Vector3 startPos;
Vector3 endPos;
bool firstInput;
public bool justJump;
public bool moveUp = true;
public bool moveDown = true;
public bool moveLeft = true;
public bool moveRight = true;
public bool didTouch = false;
public bool canPush = false;
public GameObject raycastObject;
void Update() {
}
void OnTriggerEnter(Collider other){
if (other.gameObject.tag == "UpStop"){
moveUp = false;
canPush = true;
}
if (other.gameObject.tag == "WallUpStop"){
moveUp = false;
print ("BOOYA");
//canPush = true;
}
if (other.gameObject.tag == "RightStop"){
moveRight = false;
canPush = true;
}
if (other.gameObject.tag == "LeftStop"){
moveLeft = false;
canPush = true;
}
if (other.gameObject.tag == "DownStop"){
moveDown = false;
canPush = true;
}
}
void OnTriggerExit(Collider other){
if (other.gameObject.tag == "UpStop"){
moveUp = true;
canPush = false;
}
if (other.gameObject.tag == "WallUpStop"){
moveUp = true;
print ("BOOYA");
//canPush = true;
}
if (other.gameObject.tag == "RightStop"){
moveRight = true;
canPush = false;
}
if (other.gameObject.tag == "LeftStop"){
moveLeft = true;
canPush = false;
}
if (other.gameObject.tag == "DownStop"){
moveDown = true;
canPush = false;
}
}
// Update is called once per frame
void FixedUpdate () {
if (Input.GetButtonDown ("up") || Input.GetButtonDown ("down") || Input.GetButtonDown ("left") || Input.GetButtonDown ("right")) {
if (perc == 1) {
lerpTime = 1;
currentLerpTime = 0;
firstInput = true;
justJump = true;
}
}
startPos = gameObject.transform.position;
//&& gameObject.transform.position == endPos
if (Input.GetButtonDown ("right") && moveRight == true)
{
endPos = new Vector3 (transform.position.x + 1, transform.position.y, transform.position.z );
}
if (Input.GetButtonDown ("left") && moveLeft == true)
{
endPos = new Vector3 (transform.position.x - 1, transform.position.y, transform.position.z );
}
if (Input.GetButtonDown ("up") && moveUp == true)
{
endPos = new Vector3 (transform.position.x, transform.position.y, transform.position.z + 1);
}
if (Input.GetButtonDown ("down") && moveDown == true)
{
endPos = new Vector3 (transform.position.x, transform.position.y, transform.position.z - 1);
}
if (Input.GetButtonDown ("right"))
{
gameObject.transform.rotation = Quaternion.Euler (0,90,0);
}
if (Input.GetButtonDown ("left"))
{
gameObject.transform.rotation = Quaternion.Euler (0,-90,0);
}
if (Input.GetButtonDown ("up"))
{
gameObject.transform.rotation = Quaternion.Euler (0,0,0);
}
if (Input.GetButtonDown ("down"))
{
gameObject.transform.rotation = Quaternion.Euler (0,180,0);
}
if (firstInput == true)
{
currentLerpTime += Time.deltaTime * 5.5f;
perc = currentLerpTime / lerpTime;
gameObject.transform.position = Vector3.Lerp (startPos, endPos, perc);
if (perc > 0.8)
{
perc = 1;
}
if (Mathf.Round(perc) == 1)
{
justJump = false;
}
}
}
}
Answer by Gilles_aerts · Oct 08, 2015 at 02:57 PM
This "problem" you are experiencing is because in theory a lerped position will never reach his destination. The best solution to this is the move an empty GameObject (tile based as you are doing in your script) and then make your player lerp to that empty GameObject.
You might want to look up what the function lerp exactly does in order to understand this solution. but i'll give you and example :
lets say your player is on the position (0,0,0) .. in your code you ask it to lerp to position (1,1,0) , since you lerp the position it will never reach position (1,1,0) but it will reach something like (0.997,0.997,0) if you ask your player to move again by 1 unit you start from that (0.997,0.997,0) position and will end up something like (1.836,1.836,0) so its no longer tile based.. each time you move your player your position will get more 'messed up'
Let me know if this is not clear, then i'll try to explain it on an other way
Good luck
Your answer
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
How to move character in a grid-based area? 1 Answer
Grid based movement in 3D environment 1 Answer
How to move character horizontally with Touch, while player always runs forward? 2 Answers
Mouse drag through Maze without relying on collision 1 Answer