- Home /
Grid based movement - using transform.translate
Im trying to get the player to move from tile to tile and i got that to work already with this code. The problem is that is jumps right to the spot and i want to go there at my speed. How its set up now it goes off the x first and then goes off z, so it follows the grid of the game and doesn't just go straight through the grid.
public class Controller : MonoBehaviour { public GameObject selectedPawn;
public Color inRange;
public Color outRange;
public float range;
public float speed;
public bool selected;
private bool standing;
private bool attacking;
public bool moving;
private bool showAttackMenu;
public bool showFacing;
private GameObject[] tiles;
void Start()
{
moving = false;
standing = true;
attacking = false;
tiles = GameObject.FindGameObjectsWithTag("Tile");
}
public void SetSelectedPawn(GameObject pawn)
{
selectedPawn = pawn;
}
/*public void Move(GameObject targetTile)
{
StartCoroutine("MoveX", targetTile);
}*/
public void Move(GameObject targetTile)
{
if (!moving)
{
selectedPawn.GetComponent<SelectPawn>().ChangeAnim(2);
//print(moving + "2");
moving = true;
float travelTimeOne = targetTile.transform.position.x - selectedPawn.transform.transform.position.x;
float travelTimeTwo = targetTile.transform.position.z - selectedPawn.transform.transform.position.z;
selectedPawn.transform.Translate(travelTimeOne,0,0,Space.World);
selectedPawn.transform.Translate(0, 0, travelTimeTwo, Space.World);
/* iTween.MoveBy(selectedPawn, iTween.Hash("x", targetTile.transform.position.x - selectedPawn.transform.position.x,
"easetype", "linear", "time", travelTimeOne));
iTween.MoveBy(selectedPawn, iTween.Hash("z", targetTile.transform.position.z - selectedPawn.transform.position.z,
"time", travelTimeTwo, "delay", travelTimeOne, "easetype", "linear"));*/
DoneMoving();
CheckTiles(2);
//selectedPawn.SendMessage("DoneMoving");
moving = false;
selectedPawn.GetComponent<SelectPawn>().ChangeAnim(1);
}
}
This is getting dangerously close to pathfinding. Which is a complicated as hell topic. People have gotten degrees by writing papers on pathfinding algorithms. This is probably why your question lacks answers :)
I noticed that in these lines,
float travelTimeOne = targetTile.transform.position.x - selectedPawn.transform.transform.position.x;
float travelTimeTwo = targetTile.transform.position.z - selectedPawn.transform.transform.position.z;
you have written tranform 2 times in selectedPawn.transform.transform.position.x and selectedPawn.transform.transform.position.z.
Answer by Cyber-X-Zone · Aug 17, 2013 at 01:12 PM
While I am not sure about this and could only suggest you, but by looking at your script, I think you can use Vector3.Lerp, which is used for linearly interpolating between two vectors. Here's its format:
static function Lerp(from: Vector3, to: Vector3, t: float): Vector3;
You could set from as the position of the pawn at present and to as the position of the desired tile.
float journeyLength = Vector3.Distance(targetTile.transform.position, selectedPawn.transform.position);;
And then, just write the code as:
public void Move(GameObject targetTile)
{
if (!moving)
{
selectedPawn.GetComponent<SelectPawn>().ChangeAnim(2);
//print(moving + "2");
moving = true;
float fracJourney = distCovered / journeyLength;
transform.position = Vector3.Lerp(targetTile.transform.position, selectedPawn.transform.position, fracJourney);
}
}
I like this answer, but I would also use Update() and Time.deltaTime to animate distCovered.
Answer by Sajidfarooq · Aug 17, 2013 at 07:01 PM
Actually, as you seem to know how to use iTween, there is an elegant way to do this.
Your movement is broken down in two dimensions: a) horizontal, then b) vertical (or vice-versa). What you need is to run a), wait for it to finish, then launch b). By default however, both tweens will run at the same time if you run them from the same function.
Luckily, iTween provides a callback "oncomplete" that allows you to call-back a function when a tween has finished. So:
public void Move()
{
iTween.MoveBy(selectedPawn, iTween.Hash("x", newXPos,"time", travelTimeOne, "oncomplete", "EndHorizontalMove"));
}
public void EndHorizontalMove()
{
iTween.MoveBy(selectedPawn, iTween.Hash("z", newZPos,"time", travelTimeOne)
DoneMoving();
}
Answer by AlexKostas · Oct 02, 2017 at 04:12 PM
You can use Vector3.MoveTowards
For example:
transform.position = Vector3.MoveTowards(transform.position, pos, Time.deltaTime * speed);
Your answer
Follow this Question
Related Questions
Faults with Grid-Based movement... 2 Answers
Smooth Grid Movement 0 Answers
Adding collision detection to movement script... 1 Answer
Manual Collision Detection through triggers... 0 Answers
Move along grid where facing 1 Answer