How to sum quarters of 90º through booleans once every time.
Hello Procoders!
I hope someone can help me, I'm trying to sum quarters of 90ª through booleans while my character is doing a 360º rotation with sprites!
I have 2 problems, one of them is that if I create an int to control the rotation every frame that a bool is true the int increase +=90 but it's completely dyscontrol because it sums every frame.
The other problem is that I can control it by establishing a lot of different booleans like spin1, spin2... and when spin4 is true then we are going to post a second round and like this... to reach spin16 for example that it means the character has rotated 1440º... but it's a little crazy to do this in the code and it's not working properly due the sprites are gameobjects where they are activated or not depending on the frame for each 90º of the rotation so sometimes it no detect properly the position exact.
Any idea about how can I sum quarters of 90º through sprites?
Thanks in advance.
Answer by JVene · Aug 28, 2018 at 11:43 AM
Switch the booleans for integers, and increment by 90 * count. If, for example, you were thinking to spin 2 quarters, instead of that being the result of two bools, make it the result of an integer being 2. In that code where you'd set one of many bools, set the integer to the logically related value instead.
Hi JVene! thanks for answering.
I've tried to apply directly the integers and then do the formula, but I still have the same problem since I use a conditional to know exactly in what position is the player to follow properly the next rotation in case that the user only want to rotate for example 90º, so when I use count +=1 during that moment still sum$$anonymous$$g a lot because is in the update void.
This is how I established the code to calculate the rotation:
if (anim360.GetCurrentAnimatorStateInfo (0).IsName ("1nose")) {
oneNose = true;
twoBackCam = false;
treeTail = false;
fourFrontCam = false;
//player.count += 1; --> I want to sum just +1 and not a lot of +1.
}
if (anim360.GetCurrentAnimatorStateInfo (0).IsName ("2backcam")) {
oneNose = false;
twoBackCam = true;
treeTail = false;
fourFrontCam = false;
//player.count += 1;
}
if (anim360. +2more...
While the gameobject is available at least near a second during the animation of the sprites making the rotation of the character, the count sum a lot of times so I can not detect properly the count.
I want to sum just one any time the character do the animation, and when it is finished I will calculate with the formula. But we need to sum just +1 each time.
While I'm only about 75% certain I have a understanding of your goals, I'll try to move forward a bit on this with some assumptions. I sense you're a student, and I want to know what level of detail to write here. I'm a developer with over 30 years behind me, and on some occasions I'm answering to others new to Unity but with experience in other software development, and sometimes I'm answering to high school students who may require more detail, where I should not make many assumptions about their familiarity with design, logic and math.
It seems that 'IsName("2backcam") is a state indicator, which would match the configuration of 4 bools named oneNose, twoBackCam, treeTail, fourceFrontCam. I think treeTail would be threeTail in that pattern, and they indicate the current direction of the player.
If that's the case, this means you have 1 of 4 directions the character might face, and 1 of 4 potential rotations the character might have to rotate (though, a 4th rotation would be a full circle, so perhaps in practice the rotation is really 1 to 3 potential rotations, each being a multiple of 90 degrees.
Since the idea of a 'count' of rotations seemed to fit (you've considered using it), I think it would apply to a replacement of the 4 bools in this code, too. In this case, though, they are an enumeration. This means an integer would be used, which would be limited to 1 through 4 (zero would be meaningless). 1 faces forward (Nose), 4 is 'FrontCam'. This can be incremented and decremented with 'wrapping'. Say currentDir is the variable ins$$anonymous$$d of these bools, and it is 1. Incrementing by 2 produces 3, such that currentDir += 2 would make currentDir become 3. That's fine. Now, if currentDir += 3 is performed, the result is 6 (it was 3). Since 6 is 'out of bounds' it must be 'wrapped'. It's too large, so one limits it with something like:
if ( currentDir > 4 ) currentDir -= 4;
That 'wraps' the value of currentDir, so that the result is 2, which is correct orientation. The same for a subtraction is required:
if ( currentDir < 1 ) currentDir += 4;
If currentDir were, say, 2 and code subtracted 2, that would be zero, but zero isn't valid. Zero would be the same as 4, and this code 'wraps' to do that. Likewise, if currentDir became -1, that's also not valid, but is the same a 3, and this code wraps to that limit.
If I understand your question, and here I'm not sure, you may have that situation where the player is now facing, say 3, and you want to move it to 1. 3 - 1 = 2, so you need to rotate 2. Once rotated, it has to be limited (as above), but since 2 is valid, it can be used. If the player is facing, say 1 and you want to move to 3, 1-3 = -2. So, you decrement by 2, which gives -1, but the 'wrap' would correct that to be 3.
Hi JVene, thank you again for answering.
I'm a self-student, so probably I'm worse than a new student who use a school to improve his skills because my only "$$anonymous$$chers" are videos from the internet and forums, in fact, you are the person with more knowledge and experience in this sector that I've met, also, I can only work on my project in my spare time because I have another job to be able to live... I started from 0 in art and dev last September and my knowledge of C-sharp is very basic, (I'm working in this game: https://twitter.com/Twintipgame/status/1013767442454188034 , it's not a big thing.. but is my first game and is a completely a solo indie game..), here you can see what I have done approx and where I'm applying the rotation, but the point is how to calculate by quarters the exact rotation and with your argument from the last answer gave me new ideas about how to focus the problem, and finally after a lot of hours working and thinking about it I found a solution that seems to fit with my level!
I tried to simplify the process, and I saw (thankful to your concept about the "wrapping") that I needed to establish a condition when the count sum 1 to avoid to increase more the count in the update void, so I did a method where I've created two strings, one named oldAnim started in null, and the other named currentAnim, where I check in every moment which Anim is running, like currentAnim = "1nose" or currentAnim = "twobackcam.. threetail.." then I put a conditional saying if (oldAnim != currentAnim) then count++ and after that I did my "wrap" saying oldAnim=currentAnim, so the condition is not more true, and the sum is only just +1, and doing this it seems it works :)
I only have one thing more that I'm not understanding in all, that the first time that I do the first quarter my count sum 2 times, and after the first time when I restart the count = 0 when the character stops to spin, it works properly sum$$anonymous$$g 1 by 1. I think this has to do something about the first time the oldAnim = null, but I can not understand yet in all. $$anonymous$$y first solution of that is to establish the count the first time ever at -1, and then it works all fine.
Thank you again for all your time, you have helped me a lot :)
$$anonymous$$arc.
I'm delighted things are moving forward for you. On your last point, I think it best to start a new question with your current state of code, if you want to explore what's happening there. Sometimes what works is just fine, and there are plenty of situations where initialization values are unique.
Self study is a common theme, both with Unity and for program$$anonymous$$g generally. By the time course material is prepared the industry provides new versions of languages or operating systems, so all of us who do this are in a perpetual self learning cycle. I've been at this over 30 years, so I'm used to it. $$anonymous$$y college years are decades behind, and fortunately my study of math, physics and other related subjects are still very similar after all this time (though Quaternions were still in obscurity back then), neither C# or C++ even existed at that time, and 1 $$anonymous$$Byte of RA$$anonymous$$ (not a type, 1 million bytes) was over $1,000. I was fortunate to be in that generation where it was first possible for individuals to purchase a computer for the home.
Would you $$anonymous$$d accepting the answer, it adds points and is basically the way the site works.