Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by DeadKenny · Jan 11, 2015 at 03:44 PM · animationrotationmecanimeuleranglesdegrees

Degrees and floats to animator Blend Tree problem(Cs)

Ok so I have this blend tree that aims between up middle and low.(Since IK hands are all messy I decided to use direct animations to compensate).

These 3 animations are meant to be dependent on the rotation of the weapon which gives a value within 180 degrees but its between 360-270(top half) and 0-90 (bottom half), this is not exact but its within 180.

So the problem is that I need to convert this value to within a max of -1 to 1 to send to the animator as a float. Remember that it goes from 360-0 right in the middle of the aim so its tricky.

So far this is how I get the degrees.

 float gunRotation = gun.eulerAngles.x;
 
 //and then to try and send the float to the animator.
 
 animator.SetFloat("Aim", gunRotation); //Obviously this gunRotation needs to be divided or multiplied or something.... I don't know. This is the part I am lost at.
 
 //Hope its all clear as possible.

Comment
Add comment · Show 2
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 Mmmpies · Jan 11, 2015 at 04:09 PM 0
Share

That sounds messy but not beyond some fairly simple maths. Is 360 effectively 0 on your blend tree and which one is -1 270 or 90?

avatar image DeadKenny · Jan 11, 2015 at 04:19 PM 0
Share

Well I managed to convert using $$anonymous$$athfDeg2Rad which gets it within 0-1; I will try convert the others...

I think I might solve this. Not sure though lol. I failed maths at school.

1 Reply

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

Answer by Mmmpies · Jan 11, 2015 at 04:21 PM

OK gonna assume (which always goes well) that 270 is -1 and 90 1 and 360 = 0 but you can change the code to fit if different:

 if(gunRotation >270 && gunRotation < 360)
 {
   tempFloat = (360 - gunRotation) / 90;
   aim = 0 - tempFloat;
 }
 else if(gunRotation <= 90)
 {
   aim = gunRotation / 90;
 }
 else if(gunRotation == 360)
 {
   aim = 0;
 }

Just got your update so feel free to ignore if already solved but do post your solution if this doesn't help.

EDIT:

Then let's try some hideous Frankenstein stitching together of the 2, totally untested and edited in notepad but if your method is smooth then try this for the 270 to 360:

 animation.SetFloat("Aim", 1 - ((360 - gunRotation) * Mathf.Deg2Rad / 2f));

May have to handle 360 separately but let me know how you get on.

EDIT2:

I edited that post as I realized I had the "(1 -" and not "1 - (", that's what I get for editing in Notepad - which version did you test?

EDIT3:

OK I abandoned using my head to debug and started using unity, try this:

 // for 270 to 360
 tempFloat = 0f - (((360 - gunRotation ) * Mathf.Deg2Rad / Mathf.PI) * 2f);
 
 // for 0 to 90
 tempFloat = (gunRotation * Mathf.Deg2Rad / Mathf.PI) * 2f;
Comment
Add comment · Show 9 · 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 DeadKenny · Jan 11, 2015 at 04:26 PM 0
Share

Thanks I will try your method too.

avatar image DeadKenny · Jan 11, 2015 at 04:35 PM 0
Share

Ok your method works but it causes jarring effects.

avatar image Mmmpies · Jan 11, 2015 at 04:39 PM 0
Share

Are you sticking with your method or do you need me to update $$anonymous$$e?

If update $$anonymous$$e can you tell me more about the jarring?

avatar image DeadKenny · Jan 11, 2015 at 04:50 PM 0
Share

I don't know yet what will work, my method is also jarring but only from the 270-360 and zero, as in getting the negative value from -1 to 0. Its fine from 0-1. Smoothly goes from aim in the middle to aim down depending on the gun rotation.

Yours is jarring from any point to any point. There is no smooth flow numbers change in too large a value.

$$anonymous$$y method is like this:

 So far it looks like this:
     if(gunRotation > 0f && gunRotation < 90f){
     
     animation.SetFloat("Aim", gunRotation * $$anonymous$$athf.Deg2Rad / 2f);
     
     }
     
     //This gets it within 0-1 for the bottom half 0-90f;
 
 // Doing the same with <360 && >270 is jarring and gets everything screwed up.

So update away(if you want), it will only help those who come across this.

avatar image DeadKenny · Jan 11, 2015 at 05:13 PM 0
Share

Nope still jarring, actually its even worse, so its certainly hideous lol. It returns a positive between 0-1 not negative.

I also tried to remove the middle anim but then it doesn't flow smooth(just snaps to up or down).

What I mean by jarring is that it snaps back and forth when near zero if I set the >270 <360(top half). Your method is more jarring than $$anonymous$$e but still similar.

The animator is using mecanim and 2d Freeform Directional Blend tree with 3 anims for up middle and down.

Show more comments

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

26 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

Related Questions

Armature stuck on a single side after an animation 0 Answers

Any idea why my animation isn't working? 0 Answers

Rotate bones during mecanim animation with generic avatar 1 Answer

Top-down character running animation based on facing direction 0 Answers

How to rotate 90 degrees from 270 degrees to 0 with lerp? c# 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