I am making a 2D piano player game. I am mostly done, but I am new to Unity and programming in C#, and I am not sure how to do the next part. What do I do to make it so that when I press the record button, it records the notes and then plays it back when the play button is pressed? My script is down below. Thank you in advance for your help
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NotePlay : MonoBehaviour {
Animator anim;
public AudioClip noteA;
public AudioClip noteB;
public AudioClip noteC;
public AudioClip noteD;
public AudioClip noteE;
public AudioClip noteF;
public AudioClip noteG;
public AudioSource audio;
public string[] store;
private KeyCode lastHitKey;
// Use this for initialization
void Start() {
anim = gameObject.GetComponent<Animator>();
audio = GetComponent<AudioSource>();
}
// Update is called once per frame
void Update() {
if (Input.GetKeyDown(KeyCode.A)) {
anim.SetTrigger("A");
audio.PlayOneShot(noteA);
}
if (Input.GetKeyDown(KeyCode.B)) {
anim.SetTrigger("B");
audio.PlayOneShot(noteB);
}
if (Input.GetKeyDown(KeyCode.C)) {
audio.PlayOneShot(noteC);
anim.SetTrigger("C");
}
else if (Input.GetKeyDown(KeyCode.D)) {
anim.SetTrigger("D");
audio.PlayOneShot(noteD);
}
else if (Input.GetKeyDown(KeyCode.E)) {
anim.SetTrigger("E");
audio.PlayOneShot(noteE);
}
if (Input.GetKeyDown(KeyCode.F)) {
anim.SetTrigger("F");
audio.PlayOneShot(noteF);
}
if (Input.GetKeyDown(KeyCode.G)) {
anim.SetTrigger("G");
audio.PlayOneShot(noteG);
}
}
}
Related
I want to move multiple characters using a single animation. I tried to use loop for this but failed. Can you help? I want to return the characters I added under GameObject.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class ses : MonoBehaviour
{
AudioSource audioSource;
public Animator animator;
public GameObject[] karakul;
void Start()
{
audioSource = GetComponent<AudioSource>();
//animator = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
}
public void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "sayiCollider")
{
audioSource.Play();
foreach (var item in karakul)
{
animator.SetBool("clap", true);
}
}
else
{
animator.SetBool("clap", false);
}
}
}
enter image description here
enter image description here
enter image description here
You are iterating through the GameObjects, and every time set a param on an animator, which is nothing to do with those GameObjects.
Usually, the crowd has solved another way for performance, but to answer your question, you need to add an Animator component to all GameObject you want to animate and call the SetBool on them, something like that:
[SerializeField] private Animator[] _animators;
private void OnTiggerEnter(Collider other)
{
bool isSayi = other.CompareTag("sayiCollider");
if (isSayi) audioSource.Play();
else audioSource.Stop();
for (int i = 0; i < _animators.Length; i++) _animators[i].SetBool("clap", isSayi);
}
I'm doing a football game and i want it to play a sound every time i score a goal but i can't hear any sound.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Audio;
public class gol : MonoBehaviour
{
public AudioClip audioclip;
public AudioSource audiosource;
public bool golmu = false;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void FixedUpdate()
{
if (golmu == true && !audiosource.isPlaying)
{
audiosource.clip = audioclip;
audiosource.Play();
}
}
public void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "gol")
{
golmu = true;
}
else
{
golmu = false;
}
}
public void OnTriggerExit(Collider other)
{
if(other.gameObject.tag == "gol")
{
golmu = false;
}
}
}
I tried without using AudioClip but it's not working. Putting the AudioSource component in another object too. I tried every code I saw but it didn't work.
In the game I am creating a 'Game Over' scene which loads once the player loses the game. During the game, the score is counted by the player's position on the screen and increases as the player's y-axis position increases.
I used this code to display the score on the scene the game was actually played:
using UnityEngine;
using UnityEngine.UI;
public class PlayerScore : MonoBehaviour
{
public Transform player;
public Text scoreText;
// Update is called once per frame
void Update()
{
scoreText.text = player.position.y.ToString("0");
}
}
I tried to use the same coding to display the final score of the player on the 'Game Over' screen. The problem I faced was that I was not able to identify the player on the 'Game Over' scene as the player was not an object or sprite on that scene.
Is there an easy way to reference the player sprite from the previous scene into the final ('Game Over') scene so I can use it to determine the final score?
This is the script I tried with a game object of the 'EndScene' within the playing scene:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class EndMenu: MonoBehaviour
{
public Text scoreText;
public Transform player;
public static bool GameEnds = false;
public GameObject endMenuUI;
void OnCollisionEnter2D(Collision2D exampleCol)
{
if(exampleCol.collider.tag == "Obstacle")
{
endMenuUI.SetActive(true);
Time.timeScale = 0.0001f;
GameEnds = true;
}
}
public void Retry()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex - 1);
}
public void BackToMenu()
{
SceneManager.LoadScene("Menu");
}
public void Quit()
{
Debug.Log("Quitting game...");
Application.Quit();
}
// Update is called once per frame
void Update()
{
scoreText.text = player.position.y.ToString("0");
}
}
You can keep the player using DontDestroyOnLoad
You could add this to the player:
void Awake() {
DontDestroyOnLoad(transform.gameObject);
}
You could do other stuff like keeping just the data... but the easier way should be this one.
Keep in mind that the player will remain in you gameover screen.
In my opinion the best idea could be creating a new gameobject in your game scene called "GameOverScreen" and disable it until you need it.
I think the easiest solution would be to simply store the score as a static variable that will persist across scene loads, then manually reset it when you start over. For example:
public class PlayerScore : MonoBehaviour
{
public Transform player;
public Text scoreText;
public static string scoreString;
// Update is called once per frame
void Update()
{
scoreString = player.position.y.ToString("0");
scoreText.text = scoreString;
}
}
Now you will be able to access scoreString from anywhere in your code and it will be whatever it was when the PlayerScore component last ran its Update(). Then in your EndMenu class, you would simply update your Retry() and BackToMenu() methods like so:
public void Retry()
{
PlayerScore.scoreString = "0";
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex - 1);
}
public void BackToMenu()
{
PlayerScore.scoreString = "0";
SceneManager.LoadScene("Menu");
}
And your EndMenu.Update() method becomes the following:
// Update is called once per frame
void Update()
{
scoreText.text = PlayerScore.scoreString;
}
//try to use playerprefs to save the score
public class PlayerScore : MonoBehaviour
{
public Transform player;
public Text scoreText;
// Update is called once per frame
void Update()
{
scoreText.text = player.position.y.ToString("0");
PlayerPrefs.SetInt("CurrentScore", player.position.y);
}
}
//for another scene get the value of saved score using playerprefs.getInt
public class EndMenu: MonoBehaviour
{
public Text scoreText;
public Transform player;
public static bool GameEnds = false;
public GameObject endMenuUI;
void OnCollisionEnter2D(Collision2D exampleCol)
{
if(exampleCol.collider.tag == "Obstacle")
{
endMenuUI.SetActive(true);
Time.timeScale = 0.0001f;
GameEnds = true;
}
}
public void Retry()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex - 1);
}
public void BackToMenu()
{
SceneManager.LoadScene("Menu");
}
public void Quit()
{
Debug.Log("Quitting game...");
Application.Quit();
}
// Update is called once per frame
void Update()
{
scoreText.text = PlayerPrefs.GetInt("Score",0);
}
}
I've been trying to make a sliding door for my unity level and I've managed to set up the animations but the scripting that's supposed to link up the functions with the objects isn't working.
Here's the script for the key card:
using UnityEngine;
using System.Collections;
public class Key_Pickup_1 : MonoBehaviour {
public GameObject player;
private Player_inventory playerinventory;
void Awake ()
{
playerinventory = player.GetComponent<Player_inventory>();
}
// Update is called once per frame
void onTriggerEnter()
{
if (gameObject == player)
{
playerinventory.hasKey_1 = true;
Destroy(gameObject);
}
}
}
Here's the script for the Door animation:
using UnityEngine;
using System.Collections;
public class Door_Animation_1 : MonoBehaviour {
public string Open;
private Animator anim_1;
public GameObject player;
private Player_inventory playerInventory;
void Start()
{
anim_1 = GetComponent<Animator>();
player = GameObject.FindGameObjectWithTag("Player");
playerInventory = player.GetComponent<Player_inventory>();
}
void OntriggerEnter (Collider other)
{
if(other.gameObject == player)
{
if (playerInventory.hasKey_1)
{
anim_1.SetTrigger(Open);
}
}
}
Any Ideas?
You don't have the proper capitalization for the OnTriggerEnter methods in your code. You have two different spellings and they are both wrong. It must be exactly OnTriggerEnter (or OnTriggerEnter2D for objects with a Collider2D instead of a Collider).
I am new to Unity and I am trying to use 2 quads as buttons and one Script for playing an stopping an audio file. I have searched internet and I have not found a single solution for my problem.
this is my code
using UnityEngine;
using System.Collections;
public class PlayStop : MonoBehaviour {
public GameObject Button1;
public GameObject Button2;
public AudioClip Clip;
void Start()
{
Button1 = GameObject.Find ("Fa1Play");
Button2 = GameObject.Find ("Fa1Stop");
}
void OnMouseDown ()
{
if (Button1.GetComponent ("Fa1Play"))
{
if (!audio.isPlaying)
{
audio.clip = Clip;
audio.Play();
}
}
if (Button2.GetComponent("Fa1Stop"))
{
audio.Stop();
}
}
}
you dont need game object finds just give your buttons colliders and tag give your pause a pause tag and play a play tag
public class PlayStop : MonoBehaviour {
public AudioClip aclip;
private bool stop;
void Start()
{
audio.clip=aclip;
stop=true;
}
void Update ()
{
if(Input.touches.Length == 1)
{
Touch touchedFinger = Input.touches[0];
if(touchedFinger.phase==TouchPhase.Began){
Ray aRay = Camera.mainCamera.ScreenPointToRay(touchedFinger.position);
RaycastHit hit;
if(Physics.Raycast(aRay.origin, aRay.direction, out hit, Mathf.Infinity))
{
if(hit.collider.tag=="stop" && !stop)
{
audio.Stop();
stop=!stop
}
if(hit.collider.tag=="play" && stop)
{
audio.Play();
stop=!stop
}
}
}
}
}
}