Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Audio Clip[] Volume

Discussion in 'Scripting' started by Serveladik, May 15, 2017.

  1. Serveladik

    Serveladik

    Joined:
    Apr 19, 2017
    Posts:
    13
    Hello Guys!
    Im Newbie in Unity3d so can anybody asking how to change Volume of AudioClip?
    Heres The code
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6.  
    7. public class FPSController : MonoBehaviour
    8. {
    9.  
    10.     public float speed = 10f;
    11.     public float RunSpeed=40f;
    12.     public float sensitivity = 2f;
    13.     CharacterController player;
    14.  
    15. public AudioClip[] music;
    16. private AudioSource soundComponent;
    17. public float Volume = 0.2f;
    18.  
    19.     public GameObject eyes;
    20.  
    21.     float moveFB;
    22.     float moveLR;
    23.  
    24.     float rotX;
    25.     float rotY;
    26.     public float vertVelocity;
    27.     public float jump = 30f;
    28.     float jumpTimes;
    29.     float shift;
    30.  
    31. void Awake()
    32. {
    33. soundComponent= GetComponent<AudioSource>();
    34. soundComponent.clip = music[0];
    35. soundComponent.volume = 0.2f;
    36. soundComponent.Play();
    37. }
    38.     // Use this for initialization
    39.     void Start()
    40.     {
    41.      
    42.         soundComponent = (AudioSource)gameObject.AddComponent<AudioSource>();
    43.      
    44.         player = GetComponent<CharacterController>();
    45.  
    46.     }
    47.     //************************************************************ */
    48.  
    49.  
    50.  
    51.  
    52.  
    53.     // Update is called once per frame
    54.     void Update()
    55.     {
    56.  
    57. Volume =0.2f;
    58.  
    59. Physics.gravity = new Vector3(0, -70.0F, 0);
    60.  
    61.  
    62.         moveFB = Input.GetAxis("Vertical") * speed;
    63.         moveLR = Input.GetAxis("Horizontal") * speed;
    64.  
    65.         rotX = Input.GetAxis("Mouse X") * sensitivity;
    66.         rotY -= Input.GetAxis("Mouse Y") * sensitivity;
    67.  
    68.         rotY = Mathf.Clamp(rotY, -60f, 60f);
    69.  
    70.         Vector3 movement = new Vector3(moveLR, vertVelocity, moveFB);
    71.         transform.Rotate(0, rotX, 0);
    72.         eyes.transform.localRotation = Quaternion.Euler(rotY, 0, 0);
    73.  
    74.         movement = transform.rotation * movement;
    75.         player.Move(movement * Time.deltaTime);
    76.  
    77.      
    78.  
    79. if(Input.GetKey(KeyCode.LeftShift))
    80. {
    81.     speed=RunSpeed;
    82. }
    83. else{
    84.     speed=26f;
    85. }
    86.  
    87.         if (player.isGrounded == true)
    88.         {
    89.             jumpTimes = 0;
    90.         }
    91.         if (jumpTimes < 1)
    92.         {
    93.             if (Input.GetButtonDown("Jump"))
    94.             {
    95.                 vertVelocity += jump;
    96.                 jumpTimes += 1;
    97.             }
    98.  
    99.         }
    100.     }
    101.     void FixedUpdate()
    102.     {
    103.         if (player.isGrounded == false)
    104.         {
    105.             vertVelocity += Physics.gravity.y * Time.deltaTime;
    106.         }
    107.         else
    108.         {
    109.             vertVelocity = 0f;
    110.         }
    111.     }
    112.     void OnTriggerEnter( Collider other )
    113. {
    114.     if (other.gameObject.tag == "House")
    115.     {
    116. Volume=Volume/2;
    117.  
    118.      
    119. }
    120.       }
    121.              void OnTriggerExit( Collider other )
    122. {
    123.     if (other.gameObject.tag == "House")
    124.     {
    125.                  soundComponent.volume=Volume*2;
    126.      
    127.  
    128.  
    129.  
    130.  
    131.  
    132.             }
    133.  
    134.  
    135.      }
    136.    
    137. }
    138.  
     
    Last edited: May 15, 2017
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,748
    Use [code ]code tags[/code].
     
  3. Serveladik

    Serveladik

    Joined:
    Apr 19, 2017
    Posts:
    13
    Done))
    Ty
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,748
    You're using your "Volume" variable inconsistently. You should be able to get rid of that entirely, and use only soundComponent.volume instead.