I am trying to display player tutorial when the user 1st starts the game using playerpref and want the game to be paused, the problem i am facing is that Time.timescale=0 is not pausing the game when placed inside start (tutorialCanvas gets displayed), but works when is called by a button(Pause button).
Following is the code I use
void Start()
{
if (PlayerPrefs.HasKey ("test4") ==false ) {
tutorialCanvas.SetActive (true);
Time.timeScale = 0;
}
}
Time.timeScale = 0 is really messed up for me so what i could recommend you is that if you just want to pause something like pausing a movement of a character then you can try it like this :
GameObject PlayerScript;
if(Input.GetKey(KeyCode.P)){
//lets disable the playermovement script only not the whole object
PlayerScript = GetComponent<PlayerMovement>().enabled = false;
}
I've been stucked there also so i made an alternative way. And you can also visit this one. If you want a free of that asset you can get it here
I had this issue recently, although it was only doing it on a build, and not in the editor. Changing timeScale to something like 0.0001f would fix it, but that wasn't a great solution. Creating an input settings asset (under Camera -> Player Input -> Open Input Settings) seemed to have fixed it for the builds.
Related
I've been studying c sharp as part of my games course and my teacher has left and they cant find a replacement so I'm really struggling with coding my demo game. I’ve got a problem with my doors in my game and I was wondering if anyone knew how u can get it to work because I haven’t really got a clue when it comes to coding. Basically what I’ve got is my doors are shut then when I start my game they open and then shut but I need a code that will make sure it says shut until I’m in the trigger point but all the YouTube clips haven't been working and it says it’s got problems with it. I know what I want it to do I just don’t know how to put that into action.
This is my code but I don't think it does anything:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DoorAnimation: MonoBehaviour
{
Animator animator;
int isOpeningHash;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
animator = GetComponent<Animator>();
isOpeningHash = Animator.StringToHash("isOpening");
bool isOpening = animator.GetBool(isOpeningHash);
bool OpenPressed = Input.GetKey("e");
bool CloseDoor = Input.GetKey("left shift");
if (!isOpening && OpenPressed)
{
animator.SetBool(isOpeningHash, true);
}
if (isOpening && OpenPressed)
{
animator.SetBool(isOpeningHash, false);
}
}
}
First of all, make 2 animations(and turn off the loop or it will loop for eternity) for the opening and closing of door. Don't make Bools for the animator. It's basically useless. At the top of a script declare a bool called as open and set it equal to false(if the door is closed from start). Check if E is pressed and open is set to false(the door is closed), play the opening animation, set open to true and if left shift is pressed and open is set to true(the door is open) play the closing animation, set open to false. You can play animations if a certain key is pressed like this https://answers.unity.com/questions/1362883/how-to-make-an-animation-play-on-keypress-unity-ga.html
This should do your job :)
I have a Unity game that runs on both Android Cardboard and Oculus Go. I'm trying to determine whether the Go's controller is connected.
I imported the Oculus integration package from the Unity asset store (though I'm not sure it's actually required... I've gotten the impression that Oculus support has been built into Unity since at least 2018.3, if not 2018.2 or earlier). I also deleted Cardboard and added Oculus as Virtual Reality SDKs in Player settings.
The following code executes in the Start() method that initializes most of my game:
void Start() {
// ...
if (OVRInput.IsControllerConnected(OVRInput.Controller.RTrackedRemote)) {
// do something visible
}
// ...
}
The problem is, OVRInput.IsControllerConnected(...) always returns false, and the code inside the block never executes.
Other things I've tried:
Moved the call to OVRInput.IsControllerConnected() from Start() to Update(), just in case it's an initialization-time issue. No success. Same result.
Instead of using OVRInput.Controller.RTrackedRemote as the argument, I tried the other objects... LTrackedRemote, Active, All, Gamepad, LTouch, RTouch, Remote, Touch, Touchpad, and None. All of them except '.None' returned false. '.None' returned true.
I set a breakpoint on the line calling OVRInput.IsControllerConnected() (after moving it to Update()), then called OVRInput.GetConnectedControllers() in VS2017's immediate window... it returned "None". Ditto for OVRInput.GetActiveController().
The game itself began as Android Cardboard. So far, the only major changes I've made to it are:
Importing the Oculus support library from Unity's Asset store.
In Player -> XR Settings, I deleted "Cardboard" and added "Oculus" as a VR SDK
In Build Settings, I changed the build method from 'Gradle' to 'Internal' (Gradle builds failed... I've seen posts from summer 2018 saying it's a Unity bug, but I'm not sure whether that's still current info... regardless, changing from Gradle to Internal made THAT error go away).
Most notably, I have NOT added any Oculus-specific prefabs, or changed/removed any of the GoogleVR-specific prefabs.
I know you tried moving IsControllerConnected to Update but did you try GetConnectedControllers in Update after a second? That's what did the trick for me. So in Update():
// initialize hand once after one second of start
if(!handInitialised){
initialWait += Time.deltaTime;
if(initialWait > 1f){
OVRInput.Controller c = OVRInput.GetConnectedControllers();
if(c == OVRInput.Controller.LTrackedRemote || c == OVRInput.Controller.LTouch){
//
}
//
handInitialised = true;
}
}
I'm making a game where players are put onto plates and random events happen to the plates/players. I've gone through a multitude of different networking solutions like UNET, Mirror, Mirror+Steamworks P2P but none of them really worked well (main issue being not being able to join via IP) so I settled on Photon. After Punv2 just wouldn't work on my project (an error in PhotonEditor.CS) I just used Punv1 which is working perfectly for the most part.
The issue right now is that I'm trying to spawn players on their owned plates but they only spawn on the first plate despite each plate being owned by a player (each plate has a script that specifies which player 'owns' it. This is being set correctly). This only seems to happen when I try to test with a real player/client. If I create a fake player by just spawning in the prefab and then running it, both players will be moved to their correct plate so it seems to be a networking issue.
Here's the code responsible for moving the players to their plates.
foreach (GameObject plate in spawnedPlates)
{
//Here we loop through each plate, get the player assigned to it and move the player to that plate.
GameObject player = plate.GetComponent<Plate>().assignedTo;
PlayerClientControl playerController = player.GetComponent<PlayerClientControl>(); // originally for UNET/Mirror. Left in incase I need it(had an RPC function that moved the player).
player.transform.position = plate.transform.position + new Vector3(0, 4, 0);
}
What am I doing wrong?
EDIT:
It seems they ARE being moved to the correct positions (or atleast, it's trying to move them to the correct position) via a debug print statement that prints where they are trying to be teleported to further cementing that this is a networking issue but I have no idea how to fix it.
I assume it's something to with host/client synchronization? If anyone could shed some light on this, that'd be great because I'm pulling my hair out over this.
If you inspect the player objects at runtime, it will probably give you a clue. I had the same and in my case I had InputAction attached to my players (taken from unity StarterAssets). The first player in the game was assigned keyboard and the second a controller. I solved it by disabling all the inputaction and enabling the one of the localplayer:
private void Awake()
{
photonView = GetComponent<PhotonView>();
if (photonView.IsMine)
{
// take control of the user input by disabling all and then enabling mine
var players = FindObjectsOfType<Player>();
foreach (Player player in players)
{
player.GetComponent<PlayerInput>().enabled = false;
}
GetComponent<PlayerInput>().enabled = true;
I've made a dice app as a project to learn to work with unity (it was so good in my eyes that I put it on the google play store) but when I downloaded it from there, the Start function of at least 2 scripts isn't called and I have no idea whether the other Start functions are being called.
Here you can see 2 of the Start functions that aren't called
void Start()
{
light = light.GetComponent<Light>();
GetComponent<Button>().onClick.AddListener(TaskOnClick);
rawImage = GetComponent<RawImage>();
isLockMode = false;
rawImage.texture = getIconLock(isLockMode);
}
void Start()
{
Screen.fullScreen = false;
Dice.AddDie();
Input.gyro.enabled = true;
GlobalConfig.onShowShadowsChanged += onShadowsEnabledChange;
}
They work when I use Unity Remote on my smartphone and they also work when I just use unity without the remote...
the first script is attached to a UI element and the second script is attached to an empty GameObject called 'App'
It's also even more weird because they used to work but then I switched pc's (but used the same code).
I think something is wrong with the building itself
Found out the problem, the build was generating two files, an .apk and an other .odb or somethingl ike that (might be another extension). I had to untick 'split application binary' in the player settings
I am creating game and I need to track some statistics. I want to track total played time (since the user started game and till the moment he definitely ended game).
I have a "continue" option from main menu, so if someone turned game off and then started it and selected "Continue" - it means that I need to increment the old playedTime because, it is not new player.
How can I track this total played time? I cannot use Editor Scripts because I need this to be available just from the game build (player will start game with .exe, not from Unity)
You can store the realtimeSinceStartup on an ApplicationQuit, for example in the playerprefs. Something like this should do the trick:
using UnityEngine;
public class TimeStatistics : MonoBehaviour
{
public float TotalTime
{
get
{
float totalTime = Time.realtimeSinceStartup;
if(PlayerPrefs.HasKey("totaltime"))
totalTime += PlayerPrefs.GetFloat("totaltime");
return totalTime;
}
}
void OnApplicationQuit()
{
PlayerPregs.SetFloat("totaltime", this.TotalTime);
}
}
Make sure the script always exists, otherwise you wont get an OnApplicationQuit!