Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by xxmassassenxx · Nov 15, 2019 at 02:26 PM · animatoraidirectionvectortopdown

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

Comment
Add comment · Show 10
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TreyH · Nov 15, 2019 at 02:47 PM 1
Share

Can you screenshot your animator? Are you sure the spellings are all correct with the case?

avatar image xxmassassenxx TreyH · Nov 15, 2019 at 03:58 PM 0
Share

http://ibb.co/QkqQCRQ

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.

avatar image TreyH xxmassassenxx · Nov 15, 2019 at 04:16 PM 0
Share

Have you tried printing out your mdirection value to make sure it's nonzero?

Show more comments
avatar image mxoconnell · Nov 16, 2019 at 08:23 PM 1
Share

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?

avatar image mxoconnell mxoconnell · Nov 16, 2019 at 08:28 PM 1
Share

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.

avatar image xxmassassenxx mxoconnell · Nov 16, 2019 at 10:24 PM 0
Share

I fixed it, sorry for wasting your time

Show more comments
avatar image xxmassassenxx mxoconnell · Nov 16, 2019 at 09:03 PM 0
Share

alt text

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

skjermbilde-3.png (409.7 kB)
avatar image xxmassassenxx mxoconnell · Nov 16, 2019 at 09:20 PM 0
Share

https://filebin.net/ca5fh8e139x2vlvm

here is the link to the project

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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);
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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();




Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TreyH · Nov 15, 2019 at 03:07 PM 1
Share

It returns a value, but also changes the original btw.

avatar image xxmassassenxx · Nov 15, 2019 at 04:08 PM 0
Share

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

avatar image mxoconnell · Nov 15, 2019 at 07:54 PM 0
Share

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.

avatar image xxmassassenxx mxoconnell · Nov 15, 2019 at 09:04 PM 0
Share

i have edited my main question hope it helps

avatar image xxmassassenxx · Nov 16, 2019 at 08:14 PM 0
Share

alt text

this happens when i write that btw

skjermbilde-2.png (228.1 kB)
avatar image mxoconnell xxmassassenxx · Nov 16, 2019 at 08:19 PM 0
Share

yes i was wrong about that

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

144 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can I make an AI that avoids obstacles WITHOUT using NavMesh? 3 Answers

How should I rotate a 2D unit vector by X degrees? 1 Answer

Vector direction after collision 1 Answer

Unity2D, How do i check which way a sprite is facing? 1 Answer

Tranposing two 3D coordinates onto a 2D plane? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges