im adding perks (upgrades) to my game but they only work properly when you play alone, when multiple people are in the game, the functionality stops working.
when the player 1 (host) enters the trigger, a pop up message appears saying Press F to purchase etc etc.. but when player 2 enters the trigger, the message does not appear on his screen but on player 1's screen.
script:
public void Update()
{
if(!view.IsMine) return;
if(Input.GetKey(KeyCode.F))
{
isPressingF = true;
}
else
{
isPressingF = false;
}
}
void OnTriggerStay2D (Collider2D collider)
{
if(!view.IsMine) return;
if (!ownedHalet)
{
if(collider.gameObject.CompareTag("Player"))
{
juggConfirm.SetActive(true);
if (isPressingF)
{
if(PointSystem.Instance.points < 2500 )
return;
BuyPerkHalet();
}
}
}
}
```
Related
So I am working on a runner game and it is almost done. The problem is that I am testing the Pause Panel and when player touches a zombie, pause panel shows up and I am restarting the game again by pressing the restart button. But when I touch the zombie again panel doesnt show up and gives me the error in the title. I am stuck and any help will be appreciated. This is the code and I referenced to the line that error sends me :
[SerializeField]
private GameObject pausePanel;
[SerializeField]
private Button RestartGameButton;
[SerializeField]
private Text ScoreText;
private int score;
void Start ()
{
pausePanel.SetActive(false);
ScoreText.text = score + "M";
StartCoroutine(CountScore());
}
IEnumerator CountScore()
{
yield return new WaitForSeconds(0.6f);
score++;
ScoreText.text = score + "M";
StartCoroutine(CountScore());
}
void OnEnable()
{
PlayerDeath.endgame += PlayerDiedEndTheGame;
}
void OnDisable()
{
PlayerDeath.endgame += PlayerDiedEndTheGame;
}
void PlayerDiedEndTheGame()
{
if (!PlayerPrefs.HasKey("Score"))
{
PlayerPrefs.SetInt("Score", 0);
}
else
{
int highscore = PlayerPrefs.GetInt("Score");
if(highscore < score)
{
PlayerPrefs.SetInt("Score", score);
}
}
pausePanel.SetActive(true); //this is the line that error sends me but I cant figure it out because I didnt try to destroy the panel in the first place.
RestartGameButton.onClick.RemoveAllListeners();
RestartGameButton.onClick.AddListener(() => RestartGame());
Time.timeScale = 0f;
}
public void PauseButton()
{
Time.timeScale = 0f;
pausePanel.SetActive(true);
RestartGameButton.onClick.RemoveAllListeners();
RestartGameButton.onClick.AddListener(() => ResumeGame());
}
public void GoToMenu()
{
Time.timeScale = 1f;
SceneManager.LoadScene("MainMenu");
}
public void ResumeGame()
{
Time.timeScale = 1f;
pausePanel.SetActive(false);
}
public void RestartGame()
{
Time.timeScale = 1f;
SceneManager.LoadScene("Gameplay");
}
I found the solution. It was a simple mistake on the OnDisable method. I deleted the += sign and changed it with -= sign because it was enabling the event when it was supposed to disable.
I've made a unity3d game and I want to add UI . I've started with a pause button but it doesn't seem to work.
Here is the button info:
I've created an uiManager script to manage the button , as shown in the image above and here is the code :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class myUIManager : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public void Pause() //Function to take care of Pause Button..
{
print("Entered Pause Func");
if (Time.timeScale == 1 && paused == false) //1 means the time is normal so the game is running..
{
print("Enterer first if");
Time.timeScale = 0; //Pause Game..
}
if (Time.timeScale == 0)
{
Time.timeScale = 1; //Resume Game..
}
}
}
Here is the canvas screenshot :
Any ideas? I've been searching for hours ..
I think your problem is in your Pause method:
public void Pause() //Function to take care of Pause Button..
{
print("Entered Pause Func");
if (Time.timeScale == 1 && paused == false) //1 means the time is normal so the game is running..
{
print("Enterer first if");
Time.timeScale = 0; //Pause Game..
}
if (Time.timeScale == 0)
{
Time.timeScale = 1; //Resume Game..
}
}
If you enter the first if statement you set Time.timeScale = 0 - and then you immediately go into the second if and set it back to 1.
Try this - it returnss from the Pause method once it sets the Time.timeScale to 0.
public void Pause() //Function to take care of Pause Button..
{
print("Entered Pause Func");
if (Time.timeScale == 1 && paused == false) //1 means the time is normal so the game is running..
{
print("Enterer first if");
Time.timeScale = 0; //Pause Game..
return;
}
if (Time.timeScale == 0)
{
Time.timeScale = 1; //Resume Game..
}
}
If the only two things you want to do in your Pause method are to set the Time.timeScale to 0 or 1, you could even simplify it to this:
public void Pause() //Function to take care of Pause Button..
{
print("Entered Pause Func");
if (Time.timeScale == 1 && paused == false) //1 means the time is normal so the game is running..
{
print("Enterer first if");
Time.timeScale = 0; //Pause Game..
}
else
{
Time.timeScale = 1; //Resume Game..
}
}
if the condition of your first if statement is true then you set your timeScale to 0 then the condition of the second if becomes true then you set it back to 1 You should just change your second if to an else if so that if the first condition is true then your program wont check the second one.
public void Pause()
{
if (Time.timeScale == 1)
{
Time.timeScale = 0;
}
else if (Time.timeScale == 0)
{
Time.timeScale = 1; //Resume Game..
}
}
I'm developing in Unity, C#, and I have a bit of code that checks for player activity based on mouse position and it's working well but I'm needing to also check player activity on a touchscreen (not a mobile phone touchscreen, but a touchscreen attached to a pc). How should I modify the code that I have below to also work with touch?
private void Start()
{
InvokeRepeating("LastMousePosition", 0, _checkMousePositionTimingInterval);
}
private void Update()
{
_currentMousePosition = Input.mousePosition;
}
void LastMousePosition()
{
_prevMousePosition = Input.mousePosition;
}
void CheckPlayerIdle()
{
if (_currentMousePosition != _prevMousePosition)
UserActive = true;
else if (_currentMousePosition == _prevMousePosition)
UserActive = false;
}
Well for touch you start your inactivity timer if you don't get any touch inputs and wait for x duration.
for eg:
private void Update()
{
if(Input.touchCount == 0)
{
if(!checkingForInactivity)
{
checkingForInactivity = true;
myRoutine = StartCoroutine(CheckForInactivity());
}
}
else
{
if(checkingForInactivity) StopCoroutine(myRoutine);
}
}
Ienumrable CheckForInactivity()
{
yield new waitForSecond(3.0f);
//user is inactive
}
}
I am doing a tutorial from VR Dev School. The lesson is Picking Up an Object and Parent the Transform. This is the code I copied exactly from the lesson. I have the script and a sphere collider attached to controlller(left). I've tried toggling 'is trigger' on/off. The collision is not being detected in the console. I am not receiving any errors or warnings.
Any assistance is appreciated and I will answer any questions
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(SteamVR_TrackedObject))]
public class PickupParent : MonoBehaviour {
SteamVR_TrackedObject trackedObj;
SteamVR_Controller.Device device;
void Awake () {
trackedObj = GetComponent<SteamVR_TrackedObject>();
}
void FixedUpdate () {
device = SteamVR_Controller.Input((int)trackedObj.index);
if(device.GetTouch(SteamVR_Controller.ButtonMask.Trigger))
{
Debug.Log("You are holding 'Touch' on the trigger");
}
if (device.GetTouchDown(SteamVR_Controller.ButtonMask.Trigger))
{
Debug.Log("You activated touchdown on the trigger");
}
if (device.GetTouchUp(SteamVR_Controller.ButtonMask.Trigger))
{
Debug.Log("You activated TouchUp on the trigger");
}
if (device.GetPress(SteamVR_Controller.ButtonMask.Trigger))
{
Debug.Log("You are holding 'Press' on the trigger");
}
if (device.GetPressDown(SteamVR_Controller.ButtonMask.Trigger))
{
Debug.Log("You activated press down on the trigger");
}
if (device.GetPressUp(SteamVR_Controller.ButtonMask.Trigger))
{
Debug.Log("You activated press Up on the trigger");
}
}
void onTriggerStay(Collider col)
{
Debug.Log("You have collided with " + col.name + " and activated onTriggerStay");
if (device.GetTouch(SteamVR_Controller.ButtonMask.Trigger))
{
Debug.Log("You have collided with " + col.name + " while holding down Touch");
col.attachedRigidbody.isKinematic = true;
col.gameObject.transform.SetParent(gameObject.transform);
}
}
}
This is a simple mistake. It should be OnTriggerStay not onTriggerStay. Please capitalize the O and trigger/collision should be detected.
I almost have a working radio, where the player can walk up to it, press a button and cycle through songs. Everything works except for that with each button press the new song will play but the original will continue to play underneath it. For each new song that is played a new object is created but they are all called 'One Shot Audio,' so I don't know how to destroy them. If I can fix this bug then this should be a useful radio script for anyone who wants to use it.
Update, here is my modified radio code, now working, with the help of the answer below:
void OnTriggerEnter2D(Collider2D target) {
if (target.gameObject.tag == "radio") {
radioEnter = true;
}
}
void OnTriggerExit2D(Collider2D target) {
if (target.gameObject.tag == "radio") {
radioEnter = false;
}
}
public void radioUse(){
if ((Input.GetKeyDown (KeyCode.M)) && song3on == true && radioEnter == true) {
TurnOn ();
song1on = true;
song2on = false;
song3on = false;
}
else if ((Input.GetKeyDown (KeyCode.M)) && song1on == true && radioEnter == true) {
TurnOff ();
song1on = false;
song2on = true;
song3on = false;
TurnOn();
}
else if ((Input.GetKeyDown (KeyCode.M)) && song2on == true && radioEnter == true) {
TurnOff ();
song1on = false;
song2on = false;
song3on = true;
TurnOn ();
}
}
public void Update() {
raidoUse();
}
Since PlayClipAtPoint does not return an AudioSource to manage you should not use it for these kind of sounds. I recommend setting up your radio with one AudioSource and multiple AudioClips in a array. Just drag the clips you want to the inspector and use the public methods to control the radio. This way you can reuse it with different songs.
The following code is not tested.
Public class Radio : MonoBehaviour
{
AudioSource output;
public AudioClip[] songs;
int songIndex = 0;
void Start(){
output = gameObject.AddComponent<AudioSource>();
}
public void ToggleSong(){
songIndex++;
output.clip = songs[songIndex % songs.Length];
output.Play();
}
public void TurnOn(){
ToggleSong();
}
public void TurnOff(){
output.Stop();
}