Volume Control for Audio source from VideoClp from VideoPlayer - unity3d

I'm trying to setup a volume control slider for my video player but having a tough time trying to figure out how to control the volume in the video clip inside Unity Video Player. I can't seem to link the audio source to the video clip so I can control the volume. I don't have any problem with individual mp3 audio files but can't get it to work with video files.
Any suggestions?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Video;
using UnityEngine.EventSystems;
public class track : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
{
public AudioSource audio;
public Slider audiovolume;
public VideoPlayer video;
Slider tracking;
bool slide = false;
void Start()
{
tracking = GetComponent<Slider>();
}
public void OnPointerDown(PointerEventData a)
{
slide = true;
}
public void OnPointerUp(PointerEventData a)
{
float frame = (float)tracking.value * (float)video.frameCount;
video.frame = (long)frame;
slide = false;
}
void Update()
{
if (!slide && video.isPlaying)
tracking.value = (float)video.frame / (float)video.frameCount;
}
public void volume()
{
audio.volume = audiovolume.value;
}
}

Your code does not seem to link te audiovolume slider in any way.
Try this:
void Start()
{
tracking = GetComponent<Slider>();
if (audiovolume!=null && audio!=null)
audiovolume.onValueChanged.AddListener((x)=>audio.volume=x);
}

Related

How do I change the pitch of audio when slowing down time?

I've been making a game using Unity and I added a slow motion function. Afterwards, when I added audio, I wanted to change the pitch of all audio whenever the slow motion function ocurred, but I can't seem to do it. I've been using Brackey's audio tutorial (here if you wanna see it) to guide me into using audio in Unity
Here is my audio manager:
using UnityEngine.Audio;
using System;
public class soundManager : MonoBehaviour
{
public Sound[] sounds;
void Awake()
{
foreach(Sound s in sounds)
{
s.source = gameObject.AddComponent<AudioSource>();
s.source.clip = s.clip;
s.source.volume = s.volume;
s.source.pitch = s.pitch;
}
}
public void Play(string name)
{
Sound s = Array.Find(sounds, sound => sound.name == name);
s.source.Play();
}
}
here is my slow motion script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TimeManager : MonoBehaviour
{
public float slowdownFactor = 0.05f;
public float slowdownLength = 2.0f;
private void Update()
{
Time.timeScale += (1f / slowdownLength) * Time.unscaledDeltaTime;
Time.timeScale = Mathf.Clamp(Time.timeScale, 0f, 1f);
}
public void DoSlowMotion()
{
Time.timeScale = slowdownFactor;
Time.fixedDeltaTime = Time.timeScale * 0.02f;
}
}
I wanted to change the pitch of all audio
If you want to change the pitch of any song at runtime you can simply use the source of type AudioSource that is saved in the sound class and edit it's values directly.
If you then do this as a foreach loop, in your soundManager class, with each song in your array, you can pitch down all of them.
Change All Pitch Values:
public void ChangeAllPitchValues(float pitchDifference) {
// Loop through our whole sound array.
foreach (Sound s in sounds) {
// Adjust pitch value equal to the given difference.
s.source.pitch += pitchDifference;
}
}
Additionally it seems like you are missing the singleton pattern that Brackeys used. You should probably add that as well to easily call the soundManager from any other script including your TimeManager.
#region Singelton
public static soundManager instance;
private void Awake() {
// Check if instance is already defined
// and if this gameObject is not the current instance.
if (instance != null) {
Debug.LogWarning("Multiple instances found. Current instance was destroyed.");
Destroy (gameObject);
return;
}
instance = this;
DontDestroyOnLoad(gameObject);
foreach (Sound s in sounds) {
s.source = gameObject.AddComponent<AudioSource>();
s.source.clip = s.clip;
s.source.volume = s.volume;
s.source.pitch = s.pitch;
s.source.loop = s.loop;
}
}
#endregion
Once you've added the singleton pattern and the new function in the soundManager, then you can simply call that function from your DoSlowMotion() method.
Call the Method:
public void DoSlowMotion() {
...
// Pitch all songs down to 0.95f instead of 1f.
soundManager.instance.ChangeAllPitchValues(-slowdownFactor);
}

Camera not focused when using ARCore with Vuforia?

How do I make the camera to be focused since it is always out of focus whenever I use ARCore with vuforia library?
The ARCamera control is taken over by ARCore and we have to manually set the camera to be on autofocus mode. Adding this script to camera object worked to be continous autofocus mode.
Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Vuforia;
public class CameraFocusController : MonoBehaviour
{
void Start()
{
var vuforia = VuforiaARController.Instance;
vuforia.RegisterVuforiaStartedCallback(OnVuforiaStarted);
vuforia.RegisterOnPauseCallback(OnPaused);
}
private void OnVuforiaStarted()
{
CameraDevice.Instance.SetFocusMode(
CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO);
}
private void OnPaused(bool paused)
{
if (!paused) // resumed
{
// Set again autofocus mode when app is resumed
CameraDevice.Instance.SetFocusMode(
CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO);
}
}
}
This is the new method for the focus
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Vuforia;
public class CameraFocusController : MonoBehaviour
{
void Start()
{
var vuforia = VuforiaApplication.Instance;
vuforia.OnVuforiaStarted += OnVuforiaStarted;
vuforia.OnVuforiaPaused += OnPaused;
}
private void OnVuforiaStarted()
{
VuforiaBehaviour.Instance.CameraDevice.SetFocusMode(
Vuforia.FocusMode.FOCUS_MODE_CONTINUOUSAUTO);
}
private void OnPaused(bool paused)
{
if (!paused) // resumed
{
// Set again autofocus mode when app is resumed
VuforiaBehaviour.Instance.CameraDevice.SetFocusMode(
Vuforia.FocusMode.FOCUS_MODE_CONTINUOUSAUTO);
}
}
}

Particle system don't stop after one shot

I have a fps with a gun and when I shot the particle system continue to play . I want to do the effect of pistol shot:
I want the duration to be 0.5 sec or something
It just appears more and don't make the effect of one shot , they just fall down , what can I do?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SpawnProjectiles : MonoBehaviour
{
public GameObject firePoint;
public List<GameObject> vfx = new List<GameObject>();
private GameObject effectToSpawn;
public Button button;
// Start is called before the first frame update
void Start()
{
effectToSpawn = vfx[0];
button.onClick.AddListener(TaskOnClick);
}
// Update is called once per frame
void Update()
{
}
void SpawnVFX ()
{
GameObject vfx;
if(firePoint != null)
{
vfx = Instantiate(effectToSpawn, firePoint.transform.position, Quaternion.identity);
}else
{
Debug.Log("NoFirePoint");
}
}
void TaskOnClick()
{
SpawnVFX();
}
}
Uncheck Looping, otherwise the Particle system will keep emitting.

Control object rotation by mouse click in unity

I need help with my project in unity
I want to stop each object by clicking on it.
What I did so far:
all my objects rotate but when I click anywhere they all stop, I need them to stop only if I click on each one.
This is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EarthScript : MonoBehaviour
{
public bool rotateObject = true;
public GameObject MyCenter;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
if(rotateObject == true)
{
rotateObject = false;
}
else
{
rotateObject = true;
}
}
if(rotateObject == true)
{
Vector3 axisofRotation = new Vector3(0,1,0);
transform.RotateAround(MyCenter.transform.position,axisofRotation, 30*Time.deltaTime);
transform.Rotate(0,Time.deltaTime*30,0,Space.Self);
}
}
}
Theres two good ways to achieve this. Both ways require you to have a collider attatched to your object.
One is to raycast from the camera, through the cursor, into the scene, to check which object is currently under the cursor.
The second way is using unity's EventSystem. You will need to attach a PhysicsRaycaster on your camera, but then you get callbacks from the event system which simplifies detection (it is handled by Unity so there is less to write)
using UnityEngine;
using UnityEngine.EventSystems;
public class myClass: MonoBehaviour, IPointerClickHandler
{
public GameObject MyCenter;
public void OnPointerClick (PointerEventData e)
{
rotateObject=!rotateObject;
}
void Update()
{
if(rotateObject == true)
{
Vector3 axisofRotation = new Vector3(0,1,0);
transform.RotateAround(MyCenter.transform.position,axisofRotation, 30*Time.deltaTime);
transform.Rotate(0,Time.deltaTime*30,0,Space.Self);
}
}

Unity, Vr, Vive - Controllers To Play / Pause a Video?

I am new and currently trying to get my Vive Controllers to Pause / Play Unity. So far i Can see my "hands" and it does Recognise my triggers, which is all it needs to.
Does Anyone know how to make it Pause when I press the Trigger and then Start when I press it again ?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Valve.VR;
public class Viveinput : MonoBehaviour
{
[SteamVR_DefaultAction("Squeeze")]
public SteamVR_Action_Single squeezeAction;
public bool paused;
void Update () {
if (SteamVR_Input._default.inActions.GrabPinch.GetLastStateUp(SteamVR_Input_Sources.Any))
{
print(" Grab Pinch Up");
}
float triggerValue = squeezeAction.GetAxis(SteamVR_Input_Sources.Any);
if (triggerValue > 00f)
{
print(triggerValue);
}
}
}
This is What I am using atm for connection between controller and Unity.
I assume that your video is playing on a VideoPlayer MonoBehaviour :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Valve.VR;
public class Viveinput : MonoBehaviour
{
public VideoPlayer video;
[SteamVR_DefaultAction("Squeeze")]
public SteamVR_Action_Single squeezeAction;
private bool _triggered = false;
void Update () {
if (SteamVR_Input._default.inActions.GrabPinch.GetLastStateUp(SteamVR_Input_Sources.Any))
{
print(" Grab Pinch Up");
}
float triggerValue = squeezeAction.GetAxis(SteamVR_Input_Sources.Any);
if (triggerValue > 0f && !_triggered)
{
_triggered = true; // This will prevent the following code to be executed each frames when pressing the trigger.
if(!video.isPlaying) { // You dont need a paused boolean as the videoplayer has a property for that.
video.Play();
} else {
video.Pause();
}
} else {
_triggered = false;
}
}
}
You need to drag and drop the VideoPlayer in the editor and it should be it.