0
\$\begingroup\$

How can I move one camera between two positions along the cardinal directions?

Using W/A/S/D keys, I want to move the camera from one empty object to another on the xy plane. Those empty objects would act like destination points for the single camera object.

I don't want to assign a unique key for each destination point, so instead the W key should always go to the next object to the left, etc.

Diagram of navigation between points

When a directional key is pressed, the camera should move to the closest marker point in that direction. There can be many of these points, arranged somewhat like a grid, but the spacing is not guaranteed to be equal.

\$\endgroup\$
1
  • \$\begingroup\$ Do you plan to have a maximum distance that any two points can be apart before they aren't "connected"? I think there is a neat way of dynamically solving this with some vector math if the "scan distance" is bounded. \$\endgroup\$
    – Blockers
    Commented Jul 26, 2023 at 15:30

1 Answer 1

0
\$\begingroup\$

Try something along the lines of this:

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

public class CameraMover : MonoBehaviour
{
    public struct TransformRow {
          [SerializeField] public Transform[] row;
    }

    [SerializeField] private List<TransformRow> grid = new List<TransformRow>(); //Set this up in editor
    private Vector2Int gridPosition = new Vector2Int(0, 0);
    [SerializeField] private Camera camera;

    public Transform GetTransformAtPosition(Vector2Int position) 
    {
        return grid[position.x].row[position.y];
    }

    private void Update() 
    {
        //Put your movement code here. Adjust it so it changes gridPosition, but only once and only by one when the key is first pressed.
        camera.transform.position = GetTransformAtPosition(gridPosition).position;
    }
}

Essentially, you’re just chucking all the empty game objects that act as the points for the camera to warp to into what is essentially a 2D array, which creates the lattice you want. Then you just have to add the code to teleport the camera to the empty game object :).

Ideally you would want to refactor some parts of this code to better fit your purpose. This code is definitely not optimised nor solid, but I trust you’ll be able to optimise this solution.

\$\endgroup\$
3
  • \$\begingroup\$ The inspector won't natively edit a 2D array — do you have a recommendation for how to populate this field? \$\endgroup\$
    – DMGregory
    Commented Jul 25, 2023 at 20:15
  • \$\begingroup\$ Oh right, sorry about that. I believe the editor allows you to edit 1D arrays, so create a new class that just stores a single 1D array of game objects and replace the 2D array of game in the original class objects with a 1D array of the wrapper class you just created. Check this link for more help about the topic: discussions.unity.com/t/… \$\endgroup\$ Commented Jul 26, 2023 at 7:55
  • 1
    \$\begingroup\$ A comment in an answer that takes you to a different thread that you need to use the current standing answer is not really an answer. You might just edit your current answer to make it fully working \$\endgroup\$
    – Zibelas
    Commented Jul 26, 2023 at 8:33

You must log in to answer this question.

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