<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title><![CDATA[Infinity Code Forum — Creating location based game with 2d map]]></title>
		<link>https://forum.infinity-code.com/viewtopic.php?id=2070</link>
		<atom:link href="https://forum.infinity-code.com/extern.php?action=feed&amp;tid=2070&amp;type=rss" rel="self" type="application/rss+xml" />
		<description><![CDATA[The most recent posts in Creating location based game with 2d map.]]></description>
		<lastBuildDate>Sun, 11 Dec 2022 13:27:34 +0000</lastBuildDate>
		<generator>PunBB</generator>
		<item>
			<title><![CDATA[Re: Creating location based game with 2d map]]></title>
			<link>https://forum.infinity-code.com/viewtopic.php?pid=8780#p8780</link>
			<description><![CDATA[<p>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.<br />Otherwise, any change to the map position or zoom will make the positions of your objects wrong and you won&#039;t be able to fix it.<br />This means that all your enemies should be spawned and stored based on location, not world position.<br />How to find a location at some distance from the original one (OnlineMapsUtils.GetCoordinateInDistance):<br /><a href="https://infinity-code.com/atlas/online-maps/pages/DrawCircleAroundMarker.html">https://infinity-code.com/atlas/online- … arker.html</a></p><p>Further, when the object moves, you must change the location and not the world position.<br />See DriveToPoint example script.</p><p>When you change an object&#039;s location, position, or zoom map, you must update the object&#039;s screen or world position.<br />See UIBubblePopup example script.</p>]]></description>
			<author><![CDATA[null@example.com (Alex Vertax)]]></author>
			<pubDate>Sun, 11 Dec 2022 13:27:34 +0000</pubDate>
			<guid>https://forum.infinity-code.com/viewtopic.php?pid=8780#p8780</guid>
		</item>
		<item>
			<title><![CDATA[Re: Creating location based game with 2d map]]></title>
			<link>https://forum.infinity-code.com/viewtopic.php?pid=8778#p8778</link>
			<description><![CDATA[<p>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&#039;t stay on the map obviously. Since I cant figure out how to modify it to use OnlineMapsControlBase.GetScreenPosition. I&#039;m new to programming haha</p><p>So maybe you can show how to change it so it would work as intended with the 2d map?</p><p>The scrips works this way currently:<br />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.</p><p><strong>SpawnEnemy script added to an empty GameObject</strong><br /></p><div class="codebox"><pre><code>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&lt;FollowTarget&gt;();
        followScript.target = target;
        followScript.speed = speed;
    }

    void Update()
    {
    }
}</code></pre></div><p><strong>FollowTarget script that gets added to the spawned enemy:</strong><br /></p><div class="codebox"><pre><code>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);
    }
}</code></pre></div>]]></description>
			<author><![CDATA[null@example.com (CELL9)]]></author>
			<pubDate>Sat, 10 Dec 2022 22:31:48 +0000</pubDate>
			<guid>https://forum.infinity-code.com/viewtopic.php?pid=8778#p8778</guid>
		</item>
		<item>
			<title><![CDATA[Re: Creating location based game with 2d map]]></title>
			<link>https://forum.infinity-code.com/viewtopic.php?pid=8777#p8777</link>
			<description><![CDATA[<p>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).</p><p>Or you can use 3D map that looks like 2D map. <br />This is very easy to do - just set the camera perpendicular to the map. <br />If you are using Camera Orbit script, then just Lock Tilt - ON, Lock Pan - ON.</p>]]></description>
			<author><![CDATA[null@example.com (Alex Vertax)]]></author>
			<pubDate>Sat, 10 Dec 2022 12:19:07 +0000</pubDate>
			<guid>https://forum.infinity-code.com/viewtopic.php?pid=8777#p8777</guid>
		</item>
		<item>
			<title><![CDATA[Re: Creating location based game with 2d map]]></title>
			<link>https://forum.infinity-code.com/viewtopic.php?pid=8776#p8776</link>
			<description><![CDATA[<p>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.</p><p>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.</p>]]></description>
			<author><![CDATA[null@example.com (CELL9)]]></author>
			<pubDate>Sat, 10 Dec 2022 01:07:37 +0000</pubDate>
			<guid>https://forum.infinity-code.com/viewtopic.php?pid=8776#p8776</guid>
		</item>
		<item>
			<title><![CDATA[Re: Creating location based game with 2d map]]></title>
			<link>https://forum.infinity-code.com/viewtopic.php?pid=8775#p8775</link>
			<description><![CDATA[<p>Hello.</p><p>It depends a lot on the behavior you want to achieve.<br />If you want something to happen when the player reaches a certain location/marker, it&#039;s usually done this way:<br />You listen for the player&#039;s location change (OnlineMapsLocationServiceBase.OnLocationChanged), calculate the distance (OnlineMapsUtils.DistanceBetweenPoints) from the player to the target location, and if it&#039;s less than some threshold (eg 10 meters), then the player has reached the location.</p>]]></description>
			<author><![CDATA[null@example.com (Alex Vertax)]]></author>
			<pubDate>Fri, 09 Dec 2022 09:40:18 +0000</pubDate>
			<guid>https://forum.infinity-code.com/viewtopic.php?pid=8775#p8775</guid>
		</item>
		<item>
			<title><![CDATA[Creating location based game with 2d map]]></title>
			<link>https://forum.infinity-code.com/viewtopic.php?pid=8774#p8774</link>
			<description><![CDATA[<p>Hi! Is it possible to create a location based game using the 2d map and having the player sprite interact with the environment? I&#039;m able to set up the location service with an emulated location that shows a marker. But I don&#039;t know how to make that marker interact with other game objects. Do I have to use the 3d map to add colliders etc?</p>]]></description>
			<author><![CDATA[null@example.com (CELL9)]]></author>
			<pubDate>Thu, 08 Dec 2022 23:17:54 +0000</pubDate>
			<guid>https://forum.infinity-code.com/viewtopic.php?pid=8774#p8774</guid>
		</item>
	</channel>
</rss>
