Unity Kick Player Car Game - unity3d

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.

Related

OnTriggerEnter2D not working for my enemy health system

I have a projectile and an enemy, but I want the enemy to decrease the heath variable when it touches the projectile.
I tried to shoot projectiles but it did not decrease the health.
`using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Health : MonoBehaviour
{
public int startHealth = 20;
public int health;
// Start is called before the first frame update
void Start()
{
health = startHealth;
}
// Update is called once per frame
void OnTriggerEnter2D(Collider2D col)
{
Debug.Log("Hit");
if (col.gameObject.tag == "PlayerProjectile")
{
health = health - 1;
}
}
void LateUpdate()
{
if (health < 1)
{
Destroy(gameObject);
}
}
}
You're probably missing one or more things:
A Rigid body inside both your bullet and your enemy.
A collider (non-trigger) inside both game objects.
The layer collision matrix must match between both objects (you can learn more of this inside this document: https://docs.unity3d.com/Manual/LayerBasedCollision.html)
If the bullet is too fast, you must change the "Collision Detection Mode", also, you can learn more here: https://docs.unity3d.com/ScriptReference/Rigidbody-collisionDetectionMode.html
I hope I help you!

Get active scene when next scene loads

So I've been trying to make a game but when I press the button the scene reloads itself. What I want to do is make it so that whenever a scene is loaded it gets the build index of the active scene.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class LevelSwitch : MonoBehaviour
{
int CurrentSceneBuildIndex;
bool sceneLoaded;
void Update()
{
if (sceneLoaded == true )
{
CurrentSceneBuildIndex = SceneManager.GetActiveScene().buildIndex;
}
}
public void NextLevel()
{
SceneManager.LoadScene(CurrentSceneBuildIndex + 3 );
sceneLoaded = true;
}
public void LevelSelect()
{
SceneManager.LoadScene("LevelSelect");
}
}
The issue:
Ok, so the issue with your solution is that when a new scene is loaded in Unity, GameObjects and their attached scripts are not kept between scenes. In your scripts function NextLevel() you are loading the scene and then setting the sceneLoaded boolean to true however this is after the new scene is loaded and we know that GameObjects and their attached scripts do not transfer between scenes which means this boolean value does not transfer to the next scene either. This is unless a GameObject is marked with dontdestroyonload.
So what can we do to fix this?
To get the current build index of the scene when it is loaded you can attach a script to a GameObject in the scene you want to get the index of which uses SceneManager.sceneLoaded to detect when the scene is finished loading and then get the index.
For example:
using UnityEngine;
using UnityEngine.SceneManagement;
public class ExampleCode : MonoBehaviour
{
void OnEnable()
{
Debug.Log("OnEnable called");
SceneManager.sceneLoaded += OnSceneLoaded; // This tells the script to call the function "OnSceneLoaded" when the scene manager detects the scene has finished loading with the parameters of the scene object and the mode of how the scene was loaded
}
void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
Debug.Log("OnSceneLoaded: " + scene.name + ", Build Index : " + scene.buildIndex.ToString());
Debug.Log("Load Mode : " + mode);
}
// called third
void Start()
{
Debug.Log("Start");
}
// called when the game or scene closes
void OnDisable()
{
Debug.Log("OnDisable");
SceneManager.sceneLoaded -= OnSceneLoaded; // This tells the script to stop calling OnSceneLoaded when the scene manager detects a new scene has been loaded
}
}

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.

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.