- Home /
Way to make GameObjects rotate smoothly in turns - C# scripts behaves unexpectedly and no console error messages
Hello there,
Thanks for checking out my post. Firstly, I have two problems with my scripts, and I am not sure if I should have made a separate post for each one.
I'd also like to say that I am very new to Unity and, also, still trying to get the hang of C#, so my code will most probably contain errors and there will definitely be a more clever/elegant and shorter way to write code that has exactly the same effect I wanted (if the code worked, that is). Any suggestions for improvements and optimization or more clever use for code are welcome, as I am always trying to improve.
Now, to get to the point, I am following an online course while trying to construct a small game for the assignment. The course has you develop a basic first-person game where you shoot spinning boxes, some give you score points and some give you bonus time. I followed the course videos to complete the first level, but for the second level I decided to add some unique elements to gain some more Unity experience.
I created an arena with 4 platforms and 4 aisles (roads), each connecting one of the bigger platforms, as shown below.
The first idea is that the aisles will move up and down in a wave-like motion, starting with the middle block (I have split the aisles into 9 platforms each, so the middle platform would be platform 5) and when the first platform reaches it's specified height, the platforms adjacent to it will start being elevated, elevating the middle platform as well to be an equal height above those adjacent platforms and so on..). In more specific terms, what I really want to do is for the aisle to ascend into a curve, and once it reaches its peak, descend until it becomes level. Then descend until it becomes a reverse curve and then, from there, ascend until it becomes level again. I want this behavior to loop during the game for all of my aisles.
The second idea is to select randomly (or in turns) every few seconds one of the big platforms which will flash red (change to a red material and back to the original a few times) as a warning and then rotate 360 degrees (smoothly but fast) , knocking the player standing on it down. (There will be a collider below the map which will catch the player and re spawn him - losing a life).
For reference, here is how I organized my platforms and aisles:
For the aisles, and after some experimentation, this is the closest I could come up by using two functions {I was thinking of using four functions as in AscentFromLevel(), DescentFromLevel(), DescentToLevel(), AscentToLevel() } but I tried to make it work only by using methods Ascend() to either open the curve, negatively or positively and Descend() to close the curve and bring it to a straight line (making it level).
Here's a segment of what the ascension would look like, from a segment of the code I managed to make it work: https://giphy.com/gifs/l3vRmjJf23JiF8xI4
The problem is that, there seems, as time passes, certain variables that were supposed to be constant start becoming bigger, thus the gaps between the platforms of the aisles become larger than intended (and un-scalable without a Jump function for the player).
This is the end result: https://giphy.com/gifs/l2JhOMBco8GCyylfW
And here's the code controlling the aisles, which will be a component of each of the 4 aisles:
The second objective was writing code that would choose one of the four platforms, every x amount of time, and rotate it 360 degrees once. I again experimented with the code a lot, and had an integer variable that can take values from 1 to 4 and flip the according platform using a rotate function on that platform. (The warning/flashing part is going to be similar code, so if I manage to do this, I can manage to do the warning bit as well). The problem is, whatever I do, the chooser will never increment, so the first platform will always be targeted. At first, platform0 would rotate indefinitely and, after changing the code to force the platform to rotate only once, will just rotate the first platform once, doing absolutely nothing afterwards.
Here is the code of the platform, which is attached as a component of Environment:
Thanks again and excuse my long post. I wanted to explain the situation as precisely as possible. Any advice is greatly appreciated!
Your answer
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
Rotate object around another object C# 2 Answers
HoloLens - Smooth Rotation doesn't work 0 Answers