- Home /
Question by
starterdev52 · Jan 24 at 05:42 PM ·
c#ui3dinteractable
How to display an image above 3D object,
I've been trying to create a script that makes a UI icon appear above some items, that rotates and scales around the player and is visible at a certain distance. I managed to make a script that does all of the above, but it suddenly started to glitch and it starts to float from the start position to the start. Can someone help me, I'm a newbie.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ItemIconScript : MonoBehaviour
{
public Image prefabUi;
private Image uiUse;
private Vector3 offset = new Vector3(0,0.35f,0);
public Camera mainCamera;
public Fps_Script objPlayer;
void Start()
{
uiUse = Instantiate(prefabUi, FindObjectOfType<Canvas>().transform).GetComponent<Image>();
}
// Update is called once per frame
void Update()
{
uiUse.transform.position = mainCamera.WorldToScreenPoint(transform.position + offset);
float dist = 1 / Vector3.Distance(transform.position, objPlayer.transform.position) * 2f;
dist = Mathf.Clamp(dist, 0.45f, 0.8f);
if(dist >= 0.6f)
uiUse.enabled = true;
else
uiUse.enabled = false;
uiUse.transform.localScale = new Vector3(dist, dist, 0);
}
}
,
Comment
Your answer
Follow this Question
Related Questions
How do you make menu settings 1 Answer
Cant access .interactable 1 Answer
Render Texture vs 3D model in screen space - camera 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers