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);
}
}
}
Related
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.
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);
}
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);
}
}
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.
Can someone please tell me how to incorporate this
code
into one below?
I really don't know what I'm doing; it does not work and just freezes. I want it the player to leave without freezing the game on the one side.
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
void OnPlayerDisconnected(NetworkPlayer player) {
Debug.Log("Clean up after player " + player);
Network.RemoveRPCs(player);
Network.DestroyPlayerObjects(player);
}
}
using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;
using UnityEngine.Networking;
public class RG_Disconnect : MonoBehaviour {
// Use this for initialization
void Start () {
GameObject lobbyManager;
lobbyManager = GameObject.Find ("LobbyManager");
if (lobbyManager != null)
Destroy(lobbyManager);
NetworkManager.Shutdown ();
}
// Update is called once per frame
void Update () {
SceneManager.LoadScene ("Garage");
}
}
Of course it freezes. You are loading the scene with name "Garage" in every frame update.
https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.LoadScene.html
LoadScene is only meant to be used once to load a specific scene.