Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by scrappybaros22 · Dec 01, 2018 at 06:30 AM · buttontriggermobilegametoggle button

Button to do two actions ?,

So as the title says, i want to make a button with the Cross Platform option for mobiles that will trigger two events, one is hiding and the other one is picking up stuff.

What should i add to the code to make it do such thing :

First Script:

 using System.Collections;
 using System.Collections.Generic;
 using System.Runtime.Remoting.Messaging;
 using UnityEngine;
 using UnityEngine.UI;
 using UnityEngine.SceneManagement;
 using UnityStandardAssets.Characters.FirstPerson;
 
 public class PlayerBehaviour : MonoBehaviour
 {
     [Header("Health Settings")]
     public GameObject healthSlider;
     public float health = 100;
     public float healthMax = 100;
     public float healValue = 5;
     public float secondToHeal = 10;
 
     [Header("Flashlight Battery Settings")]
     public GameObject Flashlight;
     public GameObject batterySlider;
     public float battery = 100;
     public float batteryMax = 100;
     public float removeBatteryValue = 20;
     public float secondToRemoveBaterry = 2;
 
     [Header("Audio Settings")]
     public AudioClip slenderNoise;
     public AudioClip cameraNoise;
 
     [Header("Page System Settings")]
     public List<GameObject> pages = new List<GameObject>();
     public int collectedPages;
 
     [Header("UI Settings")]
     public GameObject inGameMenuUI;
     public GameObject pickUpUI;
     public GameObject finishedGameUI;
     public GameObject pagesCount;
     public bool paused;
 
     void Start ()
     {
         // set initial health values
         health = healthMax;
         battery = batteryMax;
 
         healthSlider.GetComponent<Slider>().maxValue = healthMax;
         healthSlider.GetComponent<Slider>().value = healthMax;
 
         // set initial battery values
         batterySlider.GetComponent<Slider>().maxValue = batteryMax;
         batterySlider.GetComponent<Slider>().value = batteryMax;
 
         // start consume flashlight battery
         StartCoroutine(RemoveBaterryCharge(removeBatteryValue, secondToRemoveBaterry));
     }
     
     void Update ()
     {
         // update player health slider
         healthSlider.GetComponent<Slider>().value = health;
 
         // update baterry slider
         batterySlider.GetComponent<Slider>().value = battery;
 
         // if health is low than 20%
         if(health / healthMax * 100 <= 20 && health / healthMax * 100 != 0)
         {
             Debug.Log("You are dying.");
             this.GetComponent<AudioSource>().PlayOneShot(slenderNoise);
         }
 
         // if health is low than 0
         if (health / healthMax * 100 <= 0)
         {
             Debug.Log("You are dead.");
             health = 0.0f;
         }
 
         // if battery is low 50%
         if (battery / batteryMax * 100 <= 50)
         {
             Debug.Log("Flashlight is running out of battery.");
             Flashlight.transform.Find("Spotlight").gameObject.GetComponent<Light>().intensity = 2.85f;
         }
 
         // if battery is low 25%
         if (battery / batteryMax * 100 <= 25)
         {
             Debug.Log("Flashlight is almost without battery.");
             Flashlight.transform.Find("Spotlight").gameObject.GetComponent<Light>().intensity = 2.0f;
         }
 
         // if battery is low 10%
         if (battery / batteryMax * 100 <= 10)
         {
             Debug.Log("You will be out of light.");
             Flashlight.transform.Find("Spotlight").gameObject.GetComponent<Light>().intensity = 1.35f;           
         }
 
         // if battery out%
         if (battery / batteryMax * 100 <= 0)
         {
             battery = 0.00f;
             Debug.Log("The flashlight battery is out and you are out of the light.");
             Flashlight.transform.Find("Spotlight").gameObject.GetComponent<Light>().intensity = 0.0f;
         }
 
         // page system
         pagesCount.GetComponent<Text>().text = "Collected Pages: " + collectedPages + "/8";
 
         //animations
         if (Input.GetKey(KeyCode.LeftShift))
             this.gameObject.GetComponent<Animation>().CrossFade("Run", 1);
         else
             this.gameObject.GetComponent<Animation>().CrossFade("Idle", 1);
 
         // collected all pages
         if (collectedPages >= 8)
         {
             Debug.Log("You finished the game, congratulations...");
             Cursor.visible = true;
 
             // disable first person controller and show finished game UI
             this.gameObject.GetComponent<FirstPersonController>().enabled = false;
             inGameMenuUI.SetActive(false);
             finishedGameUI.SetActive(true);       
 
             // set play again button
             Button playAgainBtn = finishedGameUI.gameObject.transform.Find("PlayAgainBtn").GetComponent<Button>();
             playAgainBtn.onClick.AddListener(this.gameObject.GetComponent<MenuInGame>().PlayAgain);
 
             // set quit button
             Button quitBtn = finishedGameUI.gameObject.transform.Find("QuitBtn").GetComponent<Button>();
             quitBtn.onClick.AddListener(this.gameObject.GetComponent<MenuInGame>().QuitGame);
         } 
     }
 
     public IEnumerator RemoveBaterryCharge(float value, float time)
     {
         while (true)
         {
             yield return new WaitForSeconds(time);
 
             Debug.Log("Removing baterry value: " + value);
 
             if (battery > 0)
                 battery -= value;
             else
                 Debug.Log("The flashlight battery is out");
         }
     }
 
     public IEnumerator RemovePlayerHealth(float value, float time)
     {
         while (true)
         {
             yield return new WaitForSeconds(time);
 
             Debug.Log("Removing player health value: " + value);
 
             if (health > 0)
                 health -= value;
             else
             {
                 Debug.Log("You're dead");
                 paused = true;
                 inGameMenuUI.SetActive(true);
                 inGameMenuUI.transform.Find("ContinueBtn").gameObject.GetComponent<Button>().interactable = false;
                 inGameMenuUI.transform.Find("PlayAgainBtn").gameObject.GetComponent<Button>().interactable = true;
             }
         }
     }
 
     // function to heal player
     public IEnumerator StartHealPlayer(float value, float time)
     {
         while (true)
         {
             yield return new WaitForSeconds(time);
 
             Debug.Log("Healling player value: " + value);
 
             if (health > 0 && health < healthMax)
                 health += value;
             else
                 health = healthMax;
         }
     }
 
     // page system - show UI
     private void OnTriggerEnter(Collider collider)
     {
         // start noise when reach slender
         if (collider.gameObject.transform.tag == "Slender")
         {
             if (health > 0 && paused == false)
             {
                 this.GetComponent<AudioSource>().PlayOneShot(cameraNoise);
                 this.GetComponent<AudioSource>().loop = true;
             }            
         }
 
         if (collider.gameObject.transform.tag == "Page")
         {
             Debug.Log("You Found a Page: " + collider.gameObject.name + ", Press 'E' to pickup");
             pickUpUI.SetActive(true);      
         }
     }
 
     // page system - pickup system
     private void OnTriggerStay(Collider collider)
     {
         if (collider.gameObject.transform.tag == "Page")
         {       
             if (Input.GetKeyDown(KeyCode.E))
             {
                 Debug.Log("You get this page: " + collider.gameObject.name);
 
                 // disable UI
                 pickUpUI.SetActive(false);
 
                 // add page to list
                 pages.Add(collider.gameObject);
                 collectedPages ++;
 
                 // disable game object
                 collider.gameObject.SetActive(false);
             }
         }
     }
 
     private void OnTriggerExit(Collider collider)
     {
         // remove noise sound
         if (collider.gameObject.transform.tag == "Slender")
         {
             if (health > 0 && paused == false)
             {
                 this.GetComponent<AudioSource>().clip = null;
                 this.GetComponent<AudioSource>().loop = false;
             }          
         }
 
         // disable UI
         if (collider.gameObject.transform.tag == "Page")
             pickUpUI.SetActive(false);
     }
 }

Second Script :

 var Player : GameObject; 
 var HidingCamera : GameObject; 
 var activateTrigger : boolean = false; 
 
 function Start () {
     HidingCamera.SetActive(false);    //Disabels the the item in camera.
     Player.SetActive(true);    //turns on the item on the ground.
     activateTrigger = false;    //Activates the Trigger 
 }
 
 
 function OnTriggerExit (Col : Collider) 
 {
     if(Col.tag =="Player")
     {
         activateTrigger = false;    //Disables the Trigger 
     }
 }
 
 function OnTriggerEnter (Col : Collider) 
 {
     if(Col.tag =="Player")
     {
         activateTrigger = true;
 
         if(Input.GetKeyDown(KeyCode.E))
         {
             activateTrigger = true;
         }
     }
 }
 
 function Update () 
 {
     if (activateTrigger && Input.GetKey(KeyCode.E))
     {
         HidingCamera.SetActive(true); 
         Player.SetActive(false);
         activateTrigger = false;
     }
 }




,

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

258 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Mobile 2D touch sensitive areas on screen 0 Answers

Trying to get a toggle button to turn the accelerometer off in my game 0 Answers

Button Trigger Event 0 Answers

Slide-Out Panel Animates on Button - How to Animate on Swipe Motion on Mobile? 1 Answer

Variable value does not change 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges