Menu

Unity3D – Clamp Objects on Edges of Minimap | Clamp on Minimap Boundary | Free Scripts | Part 4

0 Comments


This is part 4 of this tutorial. If you haven’t seen the first part, click HERE.

In this part, we’ll learn how to clamp Enemy or any GameObject on the boundary of the Minimap. This has three approaches,

  1. Circular Minimap
  2. Square Minimap
  3. Irregular / Custom shape Minimap

The third approach (clamping the gameobjects on regular / Custom shape Minimap) is not included in this tutorial.

Setup:

I assume that you’re following this tutorial series from the beginning. So you’ve what we did in part 3. Or if you have just started, and your Minimap is not clamping your desired Gameobjects on the boundary of your Minimap, you’re in the right place. Let’s get our hands dirty now,

Steps (Circular Minimap):
  • First of all, let’s create a C# script. And give it a name StayInside1.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class StayInside1 : MonoBehaviour {

	public Transform MinimapCam;
	public float MinimapSize;
	Vector3 TempV3;

	void Update () {
		TempV3 = transform.parent.transform.position;
		TempV3.y = transform.position.y;
		transform.position = TempV3;
	}

	void LateUpdate () {
		// Center of Minimap
		Vector3 centerPosition = MinimapCam.transform.localPosition;

		// Just to keep a distance between Minimap camera and this Object (So that camera don't clip it out)
		centerPosition.y -= 0.5f;

		// Distance from the gameObject to Minimap
		float Distance = Vector3.Distance(transform.position, centerPosition);

		// If the Distance is less than MinimapSize, it is within the Minimap view and we don't need to do anything
		// But if the Distance is greater than the MinimapSize, then do this
		if (Distance > MinimapSize)
		{
			// Gameobject - Minimap
			Vector3 fromOriginToObject = transform.position - centerPosition;

			// Multiply by MinimapSize and Divide by Distance
			fromOriginToObject *= MinimapSize / Distance;

			// Minimap + above calculation
			transform.position = centerPosition + fromOriginToObject;
		}
	}
}

Download the Script HERE.

  • Assign this Script to the Icon (or the actual GameObject) that you want to be displayed on the Minimap and to be clamped on the edges of Minimap
  • If you don’t know how to create a minimap, or how to display icons on Minimap, watch the first part of this tutorial HERE.
  • Assign Minimap Camera and the radius of the Minimap (depends on you and the type of game you’re developing) (e.g, Minimap Size = 30)
  • Give it a try!
  • If this doesn’t work for your Gameobjects to clamp on the boundary exactly, change MinimapSize parameter in the inspector panel and test once again until you achieve it
  • That should do the trick for you (let me know if it didn’t)

That’s all for Clamping GameObjects on a Circular Minimap. For square Minimap, please continue reading,

Steps (Circular Minimap):
  • First of all, let’s create a C# script. And give it a name StayInside2.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class StayInside2 : MonoBehaviour {

	public Transform MinimapCam;
	public float MinimapSize;
	Vector3 TempV3;

	void Update () {
		TempV3 = transform.parent.transform.position;
		TempV3.y = transform.position.y;
		transform.position = TempV3;
	}

	void LateUpdate () {
		transform.position = new Vector3 (
			Mathf.Clamp(transform.position.x, MinimapCam.position.x-MinimapSize, MinimapSize+MinimapCam.position.x),
			transform.position.y,
			Mathf.Clamp(transform.position.z, MinimapCam.position.z-MinimapSize, MinimapSize+MinimapCam.position.z)
		);
	}
}

Download the Script HERE.

  • Assign this Script to the Icon (or the actual GameObject) that you want to be displayed on the Minimap and to be clamped on the edges of Minimap
  • If you don’t know how to create a minimap, or how to display icons on Minimap, watch the first part of this tutorial HERE.
  • Assign Minimap Camera and MinimapSize of the Minimap (depends on you and the type of game you’re developing) (
    if you’re confused, try to think of it in x,y coordinate system’s dimension e.g, x and y would be equal to MinimapSize that you assign in the inspector panel)
  • Give it a try!
  • If this doesn’t work for your Gameobjects to clamp on the boundary exactly, change MinimapSize parameter in the inspector panel and test once again until you achieve it
  • That should do the trick for you (let me know if it didn’t)

Watch this tutorial step by step on YoutTube HERE.

To make this Minimap circular, click HERE.

Tags: , , , , , , , , , , , , , , , , , ,

Leave a Reply

avatar
  Subscribe  
Notify of
F