Skip to content
Snippets Groups Projects
Verified Commit 1201df46 authored by Adrian Paschkowski's avatar Adrian Paschkowski :thinking:
Browse files

Spawn enemy at mouse position, not above

parent 309a4610
No related branches found
No related tags found
No related merge requests found
...@@ -70,24 +70,16 @@ public class EnemySpawnController : MonoBehaviour ...@@ -70,24 +70,16 @@ public class EnemySpawnController : MonoBehaviour
if (StatsManager.instance.ModifyMana(-1 * (int)activeSpawn.enemyCost)) if (StatsManager.instance.ModifyMana(-1 * (int)activeSpawn.enemyCost))
{ {
dragObject.GetComponent<Image>().sprite = activeSpawn.EnemySprite; dragObject.GetComponent<Image>().sprite = activeSpawn.EnemySprite;
enemyImage = Instantiate(dragObject, PositionPrefab(), Quaternion.identity, transform); enemyImage = Instantiate(dragObject, GetPreviewWorldPosition(), Quaternion.identity, transform);
} }
} }
Vector3 PositionPrefab()
{
Vector3 pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
pos.z = 0;
return pos;
}
public void OnEnemyDrag() public void OnEnemyDrag()
{ {
if (enemyImage != null) if (enemyImage != null)
{ {
enemyImage.transform.position = PositionPrefab(); enemyImage.transform.position = GetPreviewWorldPosition();
} }
} }
public void OnEnemyDrop() public void OnEnemyDrop()
...@@ -96,7 +88,19 @@ public class EnemySpawnController : MonoBehaviour ...@@ -96,7 +88,19 @@ public class EnemySpawnController : MonoBehaviour
{ {
Destroy(enemyImage); Destroy(enemyImage);
enemyImage = null; enemyImage = null;
Instantiate(activeSpawn.enemyPrefab, PositionPrefab(), Quaternion.identity); var targetPos = GetPreviewWorldPosition();
var newEnemy = Instantiate(activeSpawn.enemyPrefab, targetPos, Quaternion.identity);
// The spawned Enemy has the pivot at the bottom, not in the middle.
// Move it down by half its height to match the preview position.
targetPos.y -= newEnemy.GetComponent<SpriteRenderer>().bounds.extents.y;
newEnemy.transform.position = targetPos;
} }
} }
Vector3 GetPreviewWorldPosition()
{
Vector3 pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
pos.z = 0;
return pos;
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment