- Home /
Changing a videos frames by using a scrubber
Hi all,
I have a video that has a progress bar made from a GameObject moving from a start and end point relative to the progress of the video using Vector3.Lerp.
What I'm trying to achieve is to get the ability to click on the scrubber and drag it along its x axis to alter what frame the video is on (like you would on youtube).
Scripts I'm using are:
public class ScrubberMover : MonoBehaviour
{
public Transform startPoint;
public Transform endPoint;
public void MovePlayHead(double playedFraction)
{
transform.position = Vector3.Lerp(startPoint.position, endPoint.position, (float)playedFraction);
}
}
and
double CalculatePlayedFraction()
{
double Fraction = (double)videoPlayer.frame / (double)videoPlayer.clip.frameCount;
return Fraction;
}
Answer by davidcox70 · Apr 11, 2018 at 12:48 PM
Hello, Your first task is to add drag ability to your "play head" game object. There are lots of references to how to do this so I won't repeat here.
Then to translate the position of the dragged play head game object into a frame number, it would be something like this, added into where you handle the dragging event.
float fullDragRange = Vector3.Distance (startPoint.position, endPoint.position);
float playHeadRelativePosition = Vector3.Distance (startPoint.position, transform.position);
float playHeadFraction = playHeadRelativePosition / fullDragRange;
int targetVideoFrame=videoPlayer.frameCount*playHeadFraction;
videoPlayer.frame = targetVideoFrame;
I'm using Vector3. Distance to measure things, but if you know that the slider only operates in the X axis then you could rather just subtract transform.position.x etc.
Answer by Kat9751 · Sep 22, 2018 at 01:58 PM
Hi, if you haven't solved your problem, I think this video may help.
Your answer