0 votes

I have top down game, That I am changing to a 1/4 top down view. Created new sprite textures. Currently my object rotates and faces enemies. (I created the sprites as a 3d obj and exported the 8 pngs)

How do I instead of rotating the object change to one of the 8 direction textures that match the direction_to facing the enemy?

or would this just be easier in a 3d scene and rotate the 3d object?


Godot version 3.2.3
in Engine by (18 points)

2 Answers

–2 votes

I've written a sprite shader that rotates a sprite to face the camera along the Y-axis. The rotation itself is perfectly functional, but something strange happens when the sprite is viewed from certain specific angles.

At angles where the camera's Y rotation differs from that of the sprite's GameObject, nothing strange happens:

...But when they have matching Y rotations, (in this case, both zero) the sprite vanishes at certain values of the camera's X rotation:

Again, the vanishing only seems to happen (from what I can tell) when their Y rotations match, as you can see here:

The scale of the sprite's Transform also plays a part in what camera angles make it vanish. I've gotten the sprite to disappear a few times by just fiddling with its Z scale.

I'm really puzzled as to what's causing this. I can only assume it has something to do with how my shader does the rotating, but I can't figure out what.

Shader Code:
Code (CSharp):
Shader "Sprites/Billboard Y Only"
[PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
_Color ("Tint", Color) = (1,1,1,1)
_ScaleX ("Scale X", Float) = 1.0
_ScaleY ("Scale Y", Float) = 1.0
[MaterialToggle] PixelSnap ("Pixel snap", Float) = 0


    Cull Off
    Lighting Off
    ZWrite Off
    Blend One OneMinusSrcAlpha

        #pragma vertex vert
        #pragma fragment frag
        #pragma multi_compile _ PIXELSNAP_ON
        #include "UnityCG.cginc"

        struct appdata_t
            float4 vertex   : POSITION;
            float4 color    : COLOR;
            float2 texcoord : TEXCOORD0;

        struct v2f
            float4 vertex   : SV_POSITION;
            fixed4 color    : COLOR;
            float2 texcoord  : TEXCOORD0;

         float4 RotateAroundYInDegrees (float4 vertex, float degrees)
                 float alpha = degrees * UNITY_PI / 180.0;
                 float sina, cosa;
                 sincos(alpha, sina, cosa);
                 float2x2 m = float2x2(cosa, -sina, sina, cosa);
                 return float4(mul(m, vertex.xz), vertex.yw).xzyw;

         float4 RotateAroundYInRadians (float4 vertex, float radians)
                 float sina, cosa;
                 sincos(radians, sina, cosa);
                 float2x2 m = float2x2(cosa, -sina, sina, cosa);
                 return float4(mul(m, vertex.xz), vertex.yw).xzyw;

        fixed4 _Color;
        float _ScaleX;
        float _ScaleY;

        v2f vert(appdata_t IN)
            v2f OUT;

            IN.vertex *= float4(_ScaleX, _ScaleY, 1.0, 1.0); // Scale X and Y

            float3 camDir = mul((float3x3)unity_CameraToWorld, float3(0,0,-1));        // Camera direction
            camDir = mul((float3x3)unity_WorldToObject, camDir);                    // Put it in Object space (So object rotation won't affect it)
            camDir.y = 0.0;                                                            // Discard Y axis to make it horizontal

            float angle = acos(dot(normalize(camDir), normalize(float3(0.0, 0.0, -1.0))));    // Angle between horizontal camera direction and object forward
            if (sign(camDir.x) < 0.0) { angle *= -1; }        // Check if the angle should be negative

            OUT.vertex = UnityObjectToClipPos(RotateAroundYInRadians (IN.vertex, angle));    // Calculate rotated vertex position (And put it in Clip space)

            OUT.texcoord = IN.texcoord;
            OUT.color = IN.color * _Color;
            #ifdef PIXELSNAP_ON
            OUT.vertex = UnityPixelSnap (OUT.vertex);

            return OUT;

        sampler2D _MainTex;
        sampler2D _AlphaTex;
        float _AlphaSplitEnabled;

        fixed4 SampleSpriteTexture (float2 uv)
            fixed4 color = tex2D (_MainTex, uv);

            if (_AlphaSplitEnabled)
                color.a = tex2D (_AlphaTex, uv).r;

            return color;

        sampler2D _CameraDepthNormalsTexture;

        fixed4 frag(v2f IN) : SV_Target
            fixed4 c = SampleSpriteTexture (IN.texcoord) * IN.color;
            c.rgb *= c.a;
            return c;


Source: xetaichohang

by (38 points)
+1 vote

You can create 8 animations using your sprites, and add some logic in your script to play the animation according with rotation.

So, to fix the 3D rotation of node, you can use the Billboard mode to the sprite3D. The billboard mode makes the sprite always faces the camera.

For a AnimatedSprite3D, you can open the menu "Flags" in the inspector and enable billboard.

by (18 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.