- Home /
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.
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?
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.
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;
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?
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.
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.
Your answer
Follow this Question
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