#Fixing sprite animation bugs
1 messages · Page 1 of 1 (latest)
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
Yes its coming up as Nan In both the idle and movement tree :)
hmm weird. what is Vertical linked to in code
does it change back if you type in 0 in the animator blend tree ?
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);
}
}
Yes!
I changed the value on both blend trees to 0
does NaN appears in playmode ?
Im not sure what you mean, If you mean when i turn on playmode then no, i set the values to 0 pressed play and went back into the animater and its still 0 for both values
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
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
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
Okay done! how do i check where the object is?
click it in hirerchy and press F
Doesnt seem to do anything
maybe.. I dont normally scale it , I just clone the original and click "Mirror"
It appears like this, it moves in the right direction it just dissupears until i stop pressing the buttons
so its just the sprite?
Yes :)
oh ok what is the objects scale in playmode
you checked all those animation clips to make sure they're all fine?
pivots too
thats playmode? no tint 😏
you'd have to expand Transform
to see the scale
Havent messed around with it, just trying to learn the basics, wanted to get a handle of movement, camera movement and the tilemap feature
How do i fix it? do i just change the value to 1?
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?
yes
let me see where you do that
send code
^ save, send link
I literally just do it in the animation window :)
yeah once you change scale inside animation , animator takes a hold of those values and overrides them
so if its no -1 it might become 0
just mirror the animation itself
sorry but eould you be able to tell me how to? haha
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
Okay i removed the keyframes that changed the sale essentially just having a copy of the right animations
I cant see the mirror button anywhere in the blend tree though
screenshot your 2D composite in blend tree
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
ahh nvm I think that mirror option only works for 3d humanoid rig
maybe just scale inside the code
or flip the sprite renderer
how do i do that haha
wait try using the old mirrored ones with the scale in animator but Untik "Write Defaults" on blend tree
I mean i would just slice the sprite sheet myself and flip the images but ideally if theres an easier way id prefer that cause itll get tedious if i get more characters that dont have the left animations
doing native is ofc always best but..hmm yeah give that one I suggested a try first
this might prevent it to put its own values
That worked but now my move and idle right animations are broken 😭
the sprite just walks backwards
alr then revert back and instead do one of these two options
flipping SpriteRenderer is same as the last answer that has scale
you can try either one
is that within the gui or code?
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;
} ```
any pros or cons between using transform.localScale vs spriteRender.flipX?
I don't do 2D too often , since I only just rotate the whole object 180 on Y
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
Thank you for all the help btw!! you seem to have fixed it!
its working now ?
Yep!
awesome 
finally have a lil player that can move and put around objects haha
just need to work on the camera now
one step closer 💪
recommend you look into Cinemachine
it pretty much does all the work for you
including smoothing
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 😭
Honestly i mainly do js but i learnt a bunch of c++ years ago so coming back to c# isnt too bad
nice! at least you know the basics then
Welp..... nvm- its not fixed-
the sprite dissapearing is fixed
but the part where when you stop moving it does the directional idles isnt
did you change anything there since ?
also did you put back the Write Defaults on Blend Tree
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);
}
Yep!
why
lol
I forgot to set this up
ohh ok
yeah u need that
like you guys said to do before
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.
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
sure
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
thats pretty much what 2D light is really
except you get shadows
if you use shadow composite
How would the shadows work? from what ive seen most people just include the shadow in the pixelart
Also im assuming that also means i can do a day/night cycle? i can just change the tint with code?
awesome thanks!!
@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
I posted a short tweet for #unitytips on how to create flashy 2D lighting effects with Unity and got some requests to clarify exactly how…
this seems they used justsprites with 3d Meshes
very common
easier to do with urp though right?
I cant seem to find a example of urp used on a top down 2d game
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
@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
Are you doing 2D or 3d?
2d :)
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
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
Any guides on implementation or anything? :3
In this tutorial you will learn about the fundamental concepts used in the creation of Behaviour Trees. You will also take this knowledge and apply it in Unity by building a simple simulation scenario where a behaviour tree is used to define action sequences of a thief stealing a diamond from a gallery.