1 (edited by CELL9 2022-12-08 23:18:45)

Topic: Creating location based game with 2d map

Hi! Is it possible to create a location based game using the 2d map and having the player sprite interact with the environment? I'm able to set up the location service with an emulated location that shows a marker. But I don't know how to make that marker interact with other game objects. Do I have to use the 3d map to add colliders etc?

Re: Creating location based game with 2d map

Hello.

It depends a lot on the behavior you want to achieve.
If you want something to happen when the player reaches a certain location/marker, it's usually done this way:
You listen for the player's location change (OnlineMapsLocationServiceBase.OnLocationChanged), calculate the distance (OnlineMapsUtils.DistanceBetweenPoints) from the player to the target location, and if it's less than some threshold (eg 10 meters), then the player has reached the location.

Kind Regards,
Infinity Code Team.

Boost your productivity a lot and immediately using Ultimate Editor Enhancer. Trial and non-commerce versions available.

Re: Creating location based game with 2d map

I want to make a post apocalyptic survival type game, and one of the requirements is to have enemies that can spawn in random locations, move around and follow the player when they get within range. To do this I need to spawn them as markers right? Because when I place an object normally they will not be fixed in location when you move the map.

In the 3d map I can add a prefab with scripts as a marker, but with the 2d map it seems I can only add as a texture.

Re: Creating location based game with 2d map

If you want to use 2D map, then for the player and enemies you can use either 2D markers or position objects yourself based on the screen position (OnlineMapsControlBase.GetScreenPosition).

Or you can use 3D map that looks like 2D map.
This is very easy to do - just set the camera perpendicular to the map.
If you are using Camera Orbit script, then just Lock Tilt - ON, Lock Pan - ON.

Kind Regards,
Infinity Code Team.

Boost your productivity a lot and immediately using Ultimate Editor Enhancer. Trial and non-commerce versions available.

5 (edited by CELL9 2022-12-11 12:05:09)

Re: Creating location based game with 2d map

I have created a test script that spawns an enemy object near a set target and then follows it the normal way in the screen space. So it doesn't stay on the map obviously. Since I cant figure out how to modify it to use OnlineMapsControlBase.GetScreenPosition. I'm new to programming haha

So maybe you can show how to change it so it would work as intended with the 2d map?

The scrips works this way currently:
Add the SpawnEnemy script to an empty game object, set the target and pick an enemy prefab. On start it will spawn the enemy in a random location within a radius from the target, and gives it the FollowTarget script. It will then start moving towards the target.

SpawnEnemy script added to an empty GameObject

using UnityEngine;

public class SpawnEnemy : MonoBehaviour
{
    public Transform target; // target to follow
    public GameObject enemyPrefab; // enemy that will spawn
    public float speed = 1f; // follow speed
    public float MinSpawnDistance = 10f; // Min spawn distance
    public float MaxSpawnDistance = 20f; // Max spawn distance

    void Start()
    {
        // calculate a random spawn position within the specified distance of the target
        Vector2 spawnPos = Random.insideUnitCircle * Random.Range(MinSpawnDistance, MaxSpawnDistance);
        spawnPos += (Vector2)target.position;

        // instantiate a new enemy at the calculated spawn position
        GameObject enemy = Instantiate(enemyPrefab, spawnPos, Quaternion.identity);

        // attach the FollowTarget script to the new enemy and set the target to the existing target object
        FollowTarget followScript = enemy.AddComponent<FollowTarget>();
        followScript.target = target;
        followScript.speed = speed;
    }

    void Update()
    {
    }
}

FollowTarget script that gets added to the spawned enemy:

using UnityEngine;

public class FollowTarget : MonoBehaviour
{
    public Transform target; // target to follow
    public float speed = 1f; // follow speed


    void Start()
    { 
    }

    void Update()
    {
        // calculate the distance to the target
        float distance = Vector2.Distance(transform.position, target.position);

        // move towards the target at the specified speed
        transform.position = Vector2.MoveTowards(transform.position, target.position, speed * Time.deltaTime);
    }
}

Re: Creating location based game with 2d map

First of all, when you are working on a game that uses a map, your objects should use location instead of world or screen position.
Otherwise, any change to the map position or zoom will make the positions of your objects wrong and you won't be able to fix it.
This means that all your enemies should be spawned and stored based on location, not world position.
How to find a location at some distance from the original one (OnlineMapsUtils.GetCoordinateInDistance):
https://infinity-code.com/atlas/online- … arker.html

Further, when the object moves, you must change the location and not the world position.
See DriveToPoint example script.

When you change an object's location, position, or zoom map, you must update the object's screen or world position.
See UIBubblePopup example script.

Kind Regards,
Infinity Code Team.

Boost your productivity a lot and immediately using Ultimate Editor Enhancer. Trial and non-commerce versions available.