- Home /
Trying to implement movement similar to Mega Man Battle Network,Trying to implement specific kind of movement from Mega Man Battle Network series
Hey guys, i'm new to all this stuff and i'm trying to implement movement like in the Mega Man Battle Network series, in those games you move on 3x3 grid. Each input on the direction pad, moves you to another tile in the grid.
I have semi working code, however i'm getting weird instances where pressing in a different direction from the last direction moves you diagonally. This is my code:
private void Update() { gameObject.transform.position = IncrementInputPos(); }
public Vector3 IncrementInputPos()
{
Vector3 originPoint = new Vector3(0, 0, 0);
Vector3 playerPos = gameObject.transform.position;
if (Input.GetKeyDown(KeyCode.D))
{
playerPos += originPoint + new Vector3 (1f,playerPos.y,playerPos.z);
}
if (Input.GetKeyDown(KeyCode.A))
{
playerPos -= originPoint + new Vector3(1f, playerPos.y, playerPos.z);
}
if (Input.GetKeyDown(KeyCode.W))
{
playerPos += originPoint + new Vector3(playerPos.x, 1f, playerPos.z);
}
if (Input.GetKeyDown(KeyCode.S))
{
playerPos -= originPoint + new Vector3(playerPos.x, 1f, playerPos.z);
}
return playerPos;
}
What do you think is happening here? Also, is this the best way to implement movement considering I want the tiles in the grid to interact with the player (say if they are frozen or on fire or whatnot)?
all the best, Thanks!
maybe you are adding too much things? try:
/// <summary>
/// The distance betwwen the center of a square to the center of square next
/// </summary>
public float distanceBetween = 1f;
public Vector3 IncrementInputPos()
{
Vector3 originPoint = new Vector3(0, 0, 0);
Vector3 playerPos = gameObject.transform.position;
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.D))
{
playerPos += Vector3.right * distanceBetween;
}
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.A))
{
playerPos += Vector3.left * distanceBetween;
}
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.W))
{
playerPos += Vector3.forward * distanceBetween;
}
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.S))
{
playerPos += Vector3.right * distanceBetween;
}
return playerPos;
}
on my experience using offset variables like originPoint
made a mess on your code. if you set up the player properly at start, you can add/substract fixed positions from he, if you move the grid through scene, consider using a parent from both (grid and player) and use the same code with transform.localPosition
ins$$anonymous$$d of transform.position
Thank you for your help! I'll play around with this
I got it! It seems perhaps that feeding the position of the player into the additive equation at each key press was doing odd stuff, this worked:
public Vector3 IncrementInputPos() {
Vector3 playerPos = gameObject.transform.localPosition;
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.D))
{
playerPos = playerPos + Vector3.right;
}
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.A))
{
playerPos = playerPos + Vector3.left;
}
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.W))
{
playerPos = playerPos + Vector3.up;
}
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.S))
{
playerPos = playerPos + Vector3.down;
}
return playerPos;
}
thanks agan!
now I gotta figure out how to add clamps to this stuff