1
\$\begingroup\$

I am creating a 2d top-down game in Unity where you can press a button and the camera will cycle into different angles, mainly forward, backward, left & right. I have gotten the cycle to work by toggling on and off the tilemap renderers of rotated duplicates of the main tilemap, but the character does not translate with the "flip" of the tilemap. I am wondering how I would get the character to "flip" with the cycle.

Steps to reproduce:

  1. Create a new unity2d project

  2. Create a basic 2d character with a character controller script

  3. Setup and create a tilemap

  4. Duplicate and Rotate the tilemap by 180 on the Y and Z axis.

  5. Add this script to your player:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;

public class PerspectiveChange : MonoBehaviour
{
    // Start is called before the first frame update

    public TilemapRenderer forwardTilemap;
    public TilemapRenderer backwardTilemap;

    private bool forward = true;
    private bool backward = false;

    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

        if (Input.GetKeyDown("c"))
        {
            if (forward)
            {
                backward = true;
                backwardTilemap.enabled = true;
                forwardTilemap.enabled = false;
                forward = false;
            }

            else if (backward)
            {
                forward = true;
                forwardTilemap.enabled = true;
                backwardTilemap.enabled = false;
                backward = false;
            }
        }
    }
}
  1. Add the TilemapRenderers to the respective variables:
    enter image description here

  2. Play the game.

Edit: I should clarify: My player movement script is this:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    // public vars
    public float moveSpeed = 5f;
    public Rigidbody2D rigidbody;

    // private vars
    Vector2 movement;



    // Update is called once per frame
    void Update()
    {
        // Input
        movement.x = Input.GetAxisRaw("Horizontal");
        movement.y = Input.GetAxisRaw("Vertical");
    }

    void FixedUpdate()
    {
        // Movement
        rigidbody.MovePosition(rigidbody.position + movement * moveSpeed * Time.fixedDeltaTime);
    }
}

And the script that I use to change the perspective does this

See the problem is that the character stays in the same spot when I toggle the perspective.

\$\endgroup\$
1
  • \$\begingroup\$ when you "flip" the whole scene how do you want the controls to be? should the "forward key" still works as forward in the backward state or should it act as the "backward key"? \$\endgroup\$
    – Arian_ki
    Mar 5, 2022 at 19:44

1 Answer 1

0
\$\begingroup\$

I imagine you are using an animator component for moving your character and there's a parameter that controls the direction and it's in the following order:

  • 0 for right
  • 1 for back
  • 2 for left
  • 3 for front

and you set the value according to the key pressed at the time (e.g. "s" means 3)

So what you have to do is add an additional number (according to the current camera) to what you get from user input and then pass it to the animator.

In the "backward tilemap" example, you should add 2 to what you get as input (so if "d" was pressed, you have to pass 0 + 2 to the animator), and don't forget to use %4 as it keeps the parameter between 0 and 3

\$\endgroup\$
1
  • \$\begingroup\$ Currently, I don't have access to Unity so if there's any issues tell me here \$\endgroup\$
    – Arian_ki
    Mar 5, 2022 at 19:52

You must log in to answer this question.

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