- Home /
can't get my floats to update in the animator
i have the direction my enemy is moving by getting its current position and last position then subtracting them to get the moving direction (mdirection) then set the float parameters to the mdirections x and y values
But my floats are not updating in the animator, there are no typos
Animator: http://ibb.co/QkqQCRQ
link to download project: https://filebin.net/ca5fh8e139x2vlvm
Enemy script vvv
public Rigidbody2D rb;
public Animator animator;
private Vector3 currentPosition;
public Vector3 mdirection;
public Vector3 _lastPosition;
void Update()
{
Animate();
//sets the current position to the objects current position
currentPosition = transform.position;
//then to get the moving direction i subtract current position and last position witch
// i get after this to make it delayed by a frame
mdirection = currentPosition - _lastPosition;
//don't actualy know if normalizing does what i think
mdirection.Normalize();
_lastPosition = transform.position;
}
// this is where there is something wrong probably
void Animate()
{
if (mdirection != Vector3.zero)
{
animator.SetFloat("Horizontal", mdirection.x);
animator.SetFloat("Vertical", mdirection.y);
}
}
Player script i used for making enemy script vvv this works btw
public Vector2 movementDirection;
public float movementSpeed;
public float MOVEMENT_BASE_SPEED = 1.0f;
public Rigidbody2D rb;
public Animator animator;
void Update()
{
ProcessInputs();
Move();
Animate();
void ProcessInputs() {
movementDirection = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
movementSpeed = Mathf.Clamp(movementDirection.magnitude, 0.0f, 1.0f);
movementDirection.Normalize();
}
void Move() {
rb.velocity = movementDirection * movementSpeed * MOVEMENT_BASE_SPEED;
}
void Animate() {
if (movementDirection != Vector2.zero)
{
animator.SetFloat("Horizontal", movementDirection.x);
animator.SetFloat("Vertical", movementDirection.y);
}
animator.SetFloat("Speed", movementSpeed);
}
to make the enemy script "work" i had to change it from vector2 to vector3 and change the part with input to the moving direction
Edit: Apparantly it was working this whole time just forgot to set a speed parameter :P sorry for wasting your time guys
Can you screenshot your animator? Are you sure the spellings are all correct with the case?
Yes im sure this is almost the way i do it for my character but i use input of buttons to get direction ins$$anonymous$$d.
Have you tried printing out your mdirection
value to make sure it's nonzero?
So if I'm understanding you correctly, you are saying that the other animator works but this one does not? Can I see a picture of the one that does work? I am right now thinking that you may be changing these vertical and horizontal values, then not using them in your Blend tree. For example I can see that the tree blend has the horizontal vertical parameters, but emotions listed are different. I'm wondering if that might be something. But if your player animator works, what do you think is different between the two?
if you dont notice a difference and still cant solve it please package the unity project up into the smallest reproducible size you can and link me a download of it and I will personally help you debug the issue and post what I discover.
yes the animator in the picture^^ is working and it should be exactly the same. i dont know what you a reffering to by emotions. been thinking the processInputs has something to do with it. i will get you a link to download, the project is not very big so shouldn't take long
https://filebin.net/ca5fh8e139x2vlvm
here is the link to the project
Answer by xxmassassenxx · Nov 17, 2019 at 12:25 AM
Solved the problem by setting the float speed parameter when moving to 1 and 0 if not. and also some mathf stuff i dont know what does, but it works.
public float movementSpeed;
public float MOVEMENT_BASE_SPEED = 1.0f;
public Rigidbody2D rb;
public Animator animator;
private Vector3 currentPosition;
public Vector3 mdirection;
public Vector3 _lastPosition;
public float x;
public float y;
private void Start()
{
}
void Update()
{
Animate();
movementSpeed = Mathf.Clamp(mdirection.magnitude, 0.0f, 1.0f);
currentPosition = transform.position;
mdirection = currentPosition - _lastPosition;
x = mdirection.x;
y = mdirection.y;
_lastPosition = transform.position;
}
void Animate()
{
if (mdirection != Vector3.zero)
{
animator.SetFloat("Speed", 1);
animator.SetFloat("H", x);
animator.SetFloat("V", y);
}
else
{
animator.SetFloat("Speed", 0);
}
}
Answer by mxoconnell · Nov 15, 2019 at 03:04 PM
Describe more how it is not working. Describe the values you see when you print them. Also try breakpoints.
Normalize returns a value so that line is doing nothing, should be
mdirection = mdirection.Normalize();
This is my character script and this works, but the other one should work like this right?
public Vector2 movementDirection;
public float movementSpeed;
public float $$anonymous$$OVE$$anonymous$$ENT_BASE_SPEED = 1.0f;
public Rigidbody2D rb;
public Animator animator;
void Update()
{
ProcessInputs();
$$anonymous$$ove();
Animate();
void ProcessInputs() {
movementDirection = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
movementSpeed = $$anonymous$$athf.Clamp(movementDirection.magnitude, 0.0f, 1.0f);
movementDirection.Normalize();
}
void $$anonymous$$ove() {
rb.velocity = movementDirection * movementSpeed * $$anonymous$$OVE$$anonymous$$ENT_BASE_SPEED;
}
void Animate() {
if (movementDirection != Vector2.zero)
{
animator.SetFloat("Horizontal", movementDirection.x);
animator.SetFloat("Vertical", movementDirection.y);
}
animator.SetFloat("Speed", movementSpeed);
}
}
but the script you made dosent work but the one i have works. i need help with
animator.SetFloat("Horizontal", mdirection.x);
animator.SetFloat("Vertical", mdirection.y);
the float is right in the inspector from 0 to 1 and changes with witch way it's moving but in my animator nothing works
I'm not sure what you mean by saying that one works and one does not work. based on this new script it looks like you did a really good job of creating helper functions along the way. It's a little though, so I would prefer if you could update your question rather than posting it as a comment. So now can you post screenshots from the animator so we can see what that looks like? $$anonymous$$eep in $$anonymous$$d that when you set values the name of the value is case sensitive that has tripped me quite a few times.
i have edited my main question hope it helps
Your answer
![](https://koobas.hobune.stream/wayback/20220612214841im_/https://answers.unity.com/themes/thub/images/avi.jpg)