I want to create a map pin. Sort of like a google map pin that will stay static to my 3d object as I rotate around my object.
right now, I am using a sprite but when I rotate around my 3d object, the sprite doesn't show on the other site, I only shows when I rotate my 3d object on the side that the sprite is on.
so what can I do to have my sprite stay static and see through the model when I rotate around it?
Think of a google map pin functionality (but on a 3d object, not on a flat surface)
PS. I also want to point out that I am viewing it in a 3D world, not 2D.
Try a textured quad which faces the camera. In your scene add a 3d-Object>Quad and position it above your 3d object. Make a new material and assign the albedo your Map Pin texture, set rendering mode to transparent, fade, or cutout if you have alpha values less than 1 (i recommend fade). Assign this material to your quad by dragging the material onto the quad gameobject in the inspector. Add a script to your quad called FaceCamera
(or whatever you like), this script will update the forward of the quad's transform to always face the camera. You should now be able to rotate around your 3d object and have the map pin appear above it while facing the camera.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FaceCamera : MonoBehaviour
{
// Update is called once per frame
void Update()
{
transform.forward = (transform.position - Camera.main.transform.position);
}
}
edit: To have the map pin always appear through your 3d object you should create a shader using create>shader>unlit shader
and edit it so that for tags you have Tags { "RenderType"="Transparent" "Queue"="Transparent" }
and after Pass
but before CGPROGRAM
you add ZTest Always
and Blend SrcAlpha OneMinusSrcAlpha
. You can also rename the shader something like MapPinShader
. Assign this shader to your map pin material by going into the shader drop down unlit>'name of shader'
.
Shader "Unlit/MapPinShader"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
}
SubShader
{
Tags { "RenderType"="Transparent" "Queue"="Transparent" }
LOD 100
Pass
{
ZTest Always
ZWrite On
Blend SrcAlpha OneMinusSrcAlpha
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
// make fog work
#pragma multi_compile_fog
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
UNITY_FOG_COORDS(1)
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
float4 _MainTex_ST;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
UNITY_TRANSFER_FOG(o,o.vertex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
// sample the texture
fixed4 col = tex2D(_MainTex, i.uv);
// apply fog
UNITY_APPLY_FOG(i.fogCoord, col);
return col;
}
ENDCG
}
}
}