#Fixing sprite animation bugs

1 messages · Page 1 of 1 (latest)

harsh pine
#

@austere nova

#

Okay what do you need to see from me? :)

austere nova
#

well is vertical still coming up as NaN inside animator blend tee ?

#

first would be to find out why

#

not sure if thats the cause but you def don't want Not A Number there

harsh pine
austere nova
#

does it change back if you type in 0 in the animator blend tree ?

harsh pine
#
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public float speed = 5.0f;
    private Rigidbody2D rb;
    private Animator animator;
    private Vector2 movement;
    private Vector2 lastDirection; 

    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        animator = GetComponent<Animator>();
        lastDirection = Vector2.down;
    }

    void Update()
    {
        movement.x = Input.GetAxisRaw("Horizontal");
        movement.y = Input.GetAxisRaw("Vertical");

        if (movement.sqrMagnitude > 0.01f) 
        {
            lastDirection = movement.normalized; 
            animator.SetFloat("Horizontal", movement.x);
            animator.SetFloat("Vertical", movement.y);
        }
        else
        {
            animator.SetFloat("Horizontal", lastDirection.x);
            animator.SetFloat("Vertical", lastDirection.y);
        }

        animator.SetBool("IsMoving", movement.sqrMagnitude > 0.01f);
    }

    void FixedUpdate()
    {
        rb.MovePosition(rb.position + movement * speed * Time.fixedDeltaTime);
    }
}
harsh pine
#

I changed the value on both blend trees to 0

austere nova
harsh pine
#

If it helps im willing to share my screen on a call dont need to speak or anything if your comfortable with that, i dont have OBS installed so i cant record

austere nova
#

no worries as long as its not NaN in playmode ur fine

#

must be something else the issue

#

try to pause the game when the sprite disappears

#

and see where the object went

harsh pine
#

Maybe its to do with the scale rotation? when i was doing the animations my sprite sheet didnt have a move or idle left animation set so i just copied the right animations and set the x scale to -1

#

for all the keyframes

harsh pine
austere nova
harsh pine
austere nova
#

it should go to your object

austere nova
harsh pine
#

It appears like this, it moves in the right direction it just dissupears until i stop pressing the buttons

harsh pine
austere nova
#

oh ok what is the objects scale in playmode

austere nova
#

pivots too

austere nova
#

you'd have to expand Transform

#

to see the scale

harsh pine
harsh pine
austere nova
#

your scale is 0 for the x 😮

#

no wonder its a slit lol

harsh pine
austere nova
#

yea lol

harsh pine
# austere nova yea lol

Isnt that just a patch though? whats the underlying issue? like why was it 0 in the first place

#

the x scale changes back to 0 every time i move diagonally in the right direction

#

Im assuming its from changing the x scale to -1 when doing the animations?

austere nova
#

let me see where you do that

#

send code

#

^ save, send link

harsh pine
austere nova
#

so if its no -1 it might become 0

#

just mirror the animation itself

harsh pine
austere nova
#

should see this on the Composite

#

in blend tree

#

its also on an individual State if you click it in on animator

#

you'd have to remove any keyframes that changes your scale to make sure those values aren't overrwritten again by animator

harsh pine
#

I cant see the mirror button anywhere in the blend tree though

austere nova
harsh pine
#

wait nvm found it

#

but i cant seem to tick the box or anything

#

Im assuming pressing the grey square in the mirror collum on move left should activate the mirror?

#

but it doesnt do anything when i press on it

austere nova
#

ahh nvm I think that mirror option only works for 3d humanoid rig

#

maybe just scale inside the code

#

or flip the sprite renderer

harsh pine
austere nova
harsh pine
austere nova
#

this might prevent it to put its own values

harsh pine
#

the sprite just walks backwards

austere nova
#

flipping SpriteRenderer is same as the last answer that has scale

#

you can try either one

harsh pine
austere nova
#

eg

if(movement.x > 0)
{
transform.localScale = new Vector2(-1f, 1f);
} else if(movement.x < 0)
{
transform.localScale = new Vector2(1f, 1f);
} ```
or

```cs
if(movement.x > 0)
{
spriteRender.flipX = false;
} else if(movement.x < 0)
{
spriteRender.flipX = true;
} ```
harsh pine
austere nova
#

I think local scale might have similiar effect

#

so your player red arrow aka transform.right is technically always facing the way you're looking

#

rather than just the sprite flipping just visuals

harsh pine
#

Thank you for all the help btw!! you seem to have fixed it!

austere nova
#

its working now ?

harsh pine
austere nova
#

awesome UnityChanCelebrate

harsh pine
#

finally have a lil player that can move and put around objects haha

#

just need to work on the camera now

austere nova
#

one step closer 💪

austere nova
#

it pretty much does all the work for you

#

including smoothing

harsh pine
# austere nova one step closer 💪

Yep! ive been working on a game for about a year now just working on the story and all the details and stuff and ive finally got around to coding it and making the assets

#

Im a web dev so i hope my coding isnt too hopeless 😭

austere nova
#

hehe yeah that should help

#

do you do web c# or other language ?

harsh pine
austere nova
harsh pine
#

Welp..... nvm- its not fixed-

#

the sprite dissapearing is fixed

#

but the part where when you stop moving it does the directional idles isnt

austere nova
#

did you change anything there since ?

#

also did you put back the Write Defaults on Blend Tree

harsh pine
#

Oh yeah i removed the

if (movement.sqrMagnitude > 0.01f) 
        {
            lastDirection = movement.normalized; 
            animator.SetFloat("Horizontal", movement.x);
            animator.SetFloat("Vertical", movement.y);
        }
        else
        {
            animator.SetFloat("Horizontal", lastDirection.x);
            animator.SetFloat("Vertical", lastDirection.y);
        }
harsh pine
austere nova
#

ohh ok

harsh pine
#

thingy

austere nova
#

yeah u need that

harsh pine
#

like you guys said to do before

austere nova
#

nah I never said remove it

#

lol

harsh pine
#

Oh nvm it was someone else

#

Up here

austere nova
#

oh ok . not sure maybe they thought its 1 blend tree instead of 2 :p

if it was working fine before the change leave it how it was.

harsh pine
#

Alr!!

#

Thanks for the help!! if i run into anything else do you mind if i ping you?

#

obviously wont ping you every 5 mins dw

austere nova
#

sure

harsh pine
#

Okay so i seem to have got it working partially but what can i actually do with this? it looks like i can just tint everything

#

@austere nova

austere nova
#

except you get shadows

#

if you use shadow composite

harsh pine
harsh pine
harsh pine
#

@austere nova i found this which seems to be a better example

#

most of what ive seen seems to be for platformers and isometric 2d games

austere nova
#

very common

harsh pine
#

I cant seem to find a example of urp used on a top down 2d game

austere nova
#

2D or 3D urp makes no difference

#

at the end of the day the engine is 3D

#

even 2D is "faked"

#

its just the physics that are different engines

harsh pine
#

@austere nova any suggestions on how i could make a character(pet) sorta thing that follows my player around, in my game i want to give the character a scene where they have a choice of saving an animal which in turn lets the animal follow the character and provide abilities in fights etc etc

harsh pine
austere nova
#

A bit of a pain because obstacle avoidance. Unity has no built in A star solution for 2D

#

So maybe a third party solution like the a* project

#

Or maybe try the one navmesh on GitHub. I haven’t personally messed with the latter tho just the former

harsh pine
#

Anyway I can make my own path finding script and implement that? I was planning on doing something along those lines anyway cause I want a really detailed enemy ai

#

Don’t really know where to start with that though

austere nova
#

Ofc you can always make your own

#

The most common one is like I mentioned. a star

harsh pine
austere nova