Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Move SplineController in rhythm of song
#1
Hello, I have question about SplineController. I'm making a music arcade game like AudioSurf. I decided to use "50_EndlessRunner" scene as a start point and "RhythmTool" asset which allow to detect beats and onsets of music. RhythmTool divides song on frames (30 frames per second) and I decided to use frame value as position on spline to add objects (obstacles and powerups). It works good but problem is in Ship movement. Few days I tried to understand how to calculate Speed value of SplineController to move Ship in the rhythm of song. I found only 1 working solution is to set Speed = 0 and use Position parameter instead. I made own SplineController child class and in Update method I do this:
float nextPosition = rhythmTool.currentFrame - InfiniteTrack.Instance.lengthCut;
Position = Mathf.Lerp(PositionnextPositionTime.deltaTime 1000f);
"lengthCut" value is the total length of destroyed spline segments. I need to substract it because Spline is cutting when Ship achieves new CP point and Spline's initial position resets. 
This code works, BUT OnControlReachedPoint event never called and therefore infinite spline is not infiniteSmile As I understand events work only when Controller moves using Speed value. 
So my question is how I can calculate Speed to move Ship in rhythm of song or how I can use Position value together with events? 
As I know from school, Speed = Distance / Time. Where Distance = nextPosition - Position, Time = rhythmTool.interpolation (time between current and next frame). But this formula not working...Maybe you can help, I hope....I just stuck on this. Thanks in advance.
Reply
#2
I checked "00_SplineController" demo scene and as understood Speed is passed worldUnits per seconds (I'm using Absolute posiiton). If it's true - I can simplyfy the question: How to calculate Speed value of Controller to achieve the target position on spline using Position value. For example, controller Position = 0 and target position = 1, how to calculate speed of controller? Thanks in advance.
Reply
#3
Hi,

Speed is either in world units or relative ones based on Move Mode
Position is based on Position Mode
More about this here: https://curvyeditor.com/documentation/controllers

I didn't get the logic behind the position computation, especially the lerping using deltaTime * 1000, but I will take your computation as it is, and give you how you should compute the speed:

Code:
float nextPosition = rhythmTool.currentFrame - InfiniteTrack.Instance.lengthCut;
float realNextPosition = Mathf.Lerp(Position, nextPosition, Time.deltaTime * 1000f);
float oldPosition = Position;
Speed = (realNextPosition - oldPosition) / Time.deltaTime;


This should do the work. Let me know if it doesn't.

To clarify why I don't like the lerping you did: it will make your game frame rate dependent. In other words, if your ship moves for a second at 30fps, or a second at 60 fps, the results will be different, because Lerp(Lerp(A,B,X),C,X) is different than Lerp(A,C,2*X)
Please consider leaving a review for Curvy. This will help a lot keeping Curvy relevant in the eyes of the Asset Store algorithm.
Reply
#4
(05-11-2019, 02:48 PM)_Aka_ Wrote: Hi,

Speed is either in world units or relative ones based on Move Mode
Position is based on Position Mode
More about this here: https://curvyeditor.com/documentation/controllers

I didn't get the logic behind the position computation, especially the lerping using deltaTime * 1000, but I will take your computation as it is, and give you how you should compute the speed:

Code:
float nextPosition = rhythmTool.currentFrame - InfiniteTrack.Instance.lengthCut;
float realNextPosition = Mathf.Lerp(Position, nextPosition, Time.deltaTime * 1000f);
float oldPosition = Position;
Speed = (realNextPosition - oldPosition) / Time.deltaTime;


This should do the work. Let me know if it doesn't.

To clarify why I don't like the lerping you did: it will make your game frame rate dependent. In other words, if your ship moves for a second at 30fps, or a second at 60 fps, the results will be different, because Lerp(Lerp(A,B,X),C,X) is different than Lerp(A,C,2*X)


Hello. Thanks a lot for answer. Your solution works, but today before I checked your answer I already found working solution. It's similar but formula is:
Speed = (realNextPosition - oldPosition) * rhythmTool.interpolation * 30f;
Let me explain: 
rhythmTool.interpolation is a time between music frames. Frames are constantly 30 per second, so I mult it to 30. Is this better than division on Time.deltaTime, what do you think? music frames per seconds are always 30, not depening on game fps.
Reply
#5
so if I understand you right, rhythmTool.interpolation * 30f == 1, right?
Please consider leaving a review for Curvy. This will help a lot keeping Curvy relevant in the eyes of the Asset Store algorithm.
Reply
#6
(05-11-2019, 09:41 PM)_Aka_ Wrote: so if I understand you right, rhythmTool.interpolation * 30f == 1, right?
Not always. rhythmTool.interpolation value is between 0 and 1. But when I use it instead of "/Time.deltaTime" it looks more smooth.
Player object should always to be in current frame position and I think using of rhythmTool.interpolation guarantees it. Thanks a lot for your time and help. If I'll have any other questions about Curvy I'll let you know in this topic.
Reply
#7
You are welcome. I spend a lot of time on AudioSurf at its peak, so I am curious to see the result of your work once it is showable to the public. Good luck with your work
Please consider leaving a review for Curvy. This will help a lot keeping Curvy relevant in the eyes of the Asset Store algorithm.
Reply
#8
(05-12-2019, 11:25 PM)_Aka_ Wrote: You are welcome. I spend a lot of time on AudioSurf at its peak, so I am curious to see the result of your work once it is showable to the public. Good luck with your work
Hello, I have another one question: I'm using your ChaseCam script to follow the controller object and jump feature from "50_EndlessRunner" scene. Problem is that when player jump - camera follow it. But I want to stay camera on same height as when player run. In other words, I want the camera to follow the player, but not the jump arc (as in all runner games). Maybe you can give advice how to easily make it?
Reply
#9
Just modify the ChaseCam script to keep the transform.position.y constant.
There are different ways of doing this. One could be to modify the MoveTo position to keep it's Y coordinate constant. You can also modify the Y coordinate of the interpolated position. The core idea is to override the Y coordinate at each update.
Please consider leaving a review for Curvy. This will help a lot keeping Curvy relevant in the eyes of the Asset Store algorithm.
Reply
#10
(05-15-2019, 05:09 PM)_Aka_ Wrote: Just modify the ChaseCam script to keep the transform.position.y constant.
There are different ways of doing this. One could be to modify the MoveTo position to keep it's Y coordinate constant. You can also modify the Y coordinate of the interpolated position. The core idea is to override the Y coordinate at each update.

Thanks, but It did not help. I tried to fix Y position of Camera in LateUdate:
if (MoveTo)
{
   if (moveHeight < 0) //init fixed camera height value
   {
       moveHeight = MoveTo.position.y;
   }
   Vector3 pos = MoveTo.position;
   pos.y = moveHeight;
   
   transform.position = Vector3.SmoothDamp(transform.position, pos, ref mVelocity, ChaseTime);
}

I think the point is that the camera is not exactly behind the player (please look at the attached screenshot), and not only the Y coordinate changes during the jump. As I see all coordinates change the value, so it's harder to solve this problem.


Attached Files Image(s)
   
Reply


Possibly Related Threads...
Thread Author Replies Views Last Post
  Move the Create Mesh of a CurvyGenerator Out of the Main Parent UsernameHed 3 214 07-08-2019, 06:57 PM
Last Post: _Aka_
  Script inheriting SplineController doesn't work the same as adding component? clearleaf 1 343 04-28-2019, 12:41 PM
Last Post: _Aka_
  Move between control points Livealot 2 1,132 08-02-2018, 07:33 PM
Last Post: Livealot
  Working with, around, or against SplineController Livealot 2 999 07-20-2018, 06:45 PM
Last Post: Livealot

Forum Jump: