0
\$\begingroup\$

I'd like to implement movement exactly like it is done in this game. (You can see it in action on YouTube.)

I need my Player to always stay in the center of X and Y similar to grid movement, but with free movement/line like. Only move on x and y lines. And I need smooth changes at intersections.

 var x = Input.getAxis("Horizontal");
 var y = -Input.getAxis("Vertical");
 var deltaPos = Vector2.zero;

 if (Mathf.Abs(inputY) > Mathf.Abs(inputX)) {
     deltaPos.Y = speed * inputY * Time.deltaTime;
 }
 else {
     deltaPos.X = speed * inputX * Time.deltaTime;
 }

 transform.position += deltaPos;
\$\endgroup\$
1
  • \$\begingroup\$ I don't see an actual question, here. Are you having a problem with your current code? What have you tried? On the surface, this appears to be a 'code my game' question, where users just ask us to outright provide the code for a particular function. As a general warning, these don't always get the best reception. \$\endgroup\$
    – Gnemlock
    Commented Dec 17, 2019 at 21:54

1 Answer 1

1
\$\begingroup\$

Disabling the diagonal movement could be a good enough option. The example looks like they did that along with adding box colliders notice that the characters can be overlapped under the blocks at some points in the video. This may be enough to get you going on the right path.

void Update()
{
    TranslateNonDiagnalMovement(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
}
void TranslateNonDiagnalMovement(float x, float y)
{
    x = y > .1f | y < -.1f ? 0 : x;
    y = x > .1f | x < -.1f ? 0 : y;

    transform.Translate(x * 3, y * 3, 0);
}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .