Skip to content

ThirteeNov

My personal blog about coding and internet

Menu
  • About me
  • About Zofia Kreasi
  • Cart
  • Checkout
  • Making an airplane game from scratch in Unity
  • My account
  • Privacy Policy
  • Privacy Policy – zkLeaderboard
  • Sample Page
  • Shop
  • Tutorials on Learning JavaScript
  • ZKAccounts – Privacy Policy
Menu

How to add a 2D marker or icon on our game screen according to the 3D object in the scene in Unity

Posted on September 3, 2021November 11, 2021 by Habibie

I believe you have seen a lot some games that show a 2D icon or marker that is pointing to an object in 3D world inside the game.

In this blog post I will share a simple script that you can learn about it and develop from it to make your own HUDs for your games.

First make some 3D object on your scene. Then create an empty canvas. Add an image element inside the canvas.

Now add this script inside your Unity project. Attach it to your camera. Use your 3D object and the image inside your canvas, drag and drop them to the respective slots on camera’s script inspector window.

That’s all, try to run the game and you will see the 2D image is placed exactly on that 3D object position.

Here is the script:

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

public class ObjectPositionOnScreen : MonoBehaviour
{
	
	public Transform objectonscene;
	public GameObject spriteobject;
    
	Camera cam;
	
    // Start is called before the first frame update
    void Start()
    {
        cam = GetComponent<Camera>();
			
    }

    // Update is called once per frame
    void Update()
    {
        Vector3 screenPos = cam.WorldToScreenPoint(objectonscene.position);
		spriteobject.transform.position = new Vector3(screenPos.x,screenPos.y,0);
    }
}
Post Views: 299
ciihuy2020

Welcome!

  • My YouTube Channel
  • My GitHub Page
  • About me

Categories

  • 3DVista
  • Android
  • Apache
  • C#
  • Cordova
  • Electron & Node JS
  • HTML5, CSS & JavaScript
  • iOS
  • Let's Make Unity Games
  • Misc
  • Photoshop
  • PHP
  • Python
  • Uncategorized
  • Unity
  • WordPress

Recent Posts

  • Make objects like wires and cables easily in Unity using Ciihuy Curved Mesh
  • [SOLVED] Can’t Add Custom Domain to Blogger After Losing CNAME Verification
  • iOS App Icon Generator by CiihuyCom
  • Advanced Blinking Marker Script to show objects position in your game canvas
  • Ciihuy Images Merger – Fast & Easy Online Image Combiner
© 2025 ThirteeNov | Powered by Superbs Personal Blog theme