How to make billboard face player at a certain distance

Hi so im trying to make one of my NPC’s face the players direction at a ceirtain distance, ive used billboard Y to make the NPC face the player but i am unsure how to make it only do this when the player gets close.
any tips?
btw i am very new to game dev and coding

edit: now that ive added my blender import and made it unique the billboard y works in godot but when i launch the game/scene it does not work.
(ceirtain distance problem still happening)
help

If I understand correctly, it sounds like you’re looking for proximity-based billboarding. One option could be a shader that applies the billboard affect dynamically based on the distance.

shader_type spatial;
render_mode diffuse_lambert, cull_disabled;

uniform sampler2D albedo: source_color, hint_default_white;

void vertex() {
	float dist = distance(CAMERA_POSITION_WORLD, NODE_POSITION_WORLD);
	
	if (dist > 5.0) {
		MODELVIEW_MATRIX = VIEW_MATRIX * mat4(
			vec4(normalize(cross(vec3(0.0, 1.0, 0.0), INV_VIEW_MATRIX[2].xyz)), 0.0), 
			vec4(0.0, 1.0, 0.0, 0.0), 
			vec4(normalize(cross(INV_VIEW_MATRIX[0].xyz, 
			vec3(0.0, 1.0, 0.0))), 0.0), MODEL_MATRIX[3]
		);
	}
}

void fragment() {
	ALBEDO = texture(albedo, UV).rgb;
}

If you wanted a smoother transition that wasn’t just all or nothing, going a GDScript route would probably be better.

1 Like

thank you but where would one put this code? what part of the script?

So for a custom spatial shader, you would just create a new ShaderMaterial and point the shader property to a shader with this code.


thank you bepis i shall try this out when i get the chance :pray:

1 Like

Thank you everything worked as wanted but the proxy is the opposite of how i wanted it where you go up close and it follows you instead of being far away and following you, and also the object gets bigger for some reason.
sorry for the constant replies btw
thanks

So for reversing the effect, you can swap the distance comparison:

if (dist < 5.0) { // instead of dist > 5.0

The size shouldn’t be changing but it could appear that way with the sudden billboarding. This might be worth doing via GDScript instead since the billboard rotation would persist once it stops following the player. Really depends on what the goal is though.