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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

How to make player slow down during event?

Discussion in 'Scripting' started by exiiplays91, Apr 30, 2021.

  1. exiiplays91

    exiiplays91

    Joined:
    Mar 6, 2021
    Posts:
    2
    I'm working on this project and I want to have the player slow down while the trigger to play the audio clip is going. I'm very new to scripting so I'm not sure what I'm doing wrong. Here is the script I have. Any help is appreciated! :)
    Code (CSharp):
    1.  using UnityEngine;
    2. using System.Collections;
    3. public class PlaySound : MonoBehaviour
    4. {
    5.      public AudioClip SoundToPlay;
    6.      public float Volume;
    7.      AudioSource audio;
    8.      public bool alreadyPlayed = false;
    9.      public float movementSpeed = 10f;
    10.      void Start()
    11.      {
    12.          movementSpeed = 5f;
    13.          audio = GetComponent<AudioSource>();
    14.          movementSpeed = 10f;
    15.      }
    16.      void OnTriggerEnter()
    17.      {
    18.          if (!alreadyPlayed)
    19.          {
    20.              audio.PlayOneShot(SoundToPlay, Volume);
    21.              alreadyPlayed = true;
    22.          }
    23.      }
    24. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    Like ALL Unity functions, Start() executes all in one frame.

    If you want to slow down during a clip you can check if the clip is playing, and if so set a lower speed, if not set a higher speed.
     
  3. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,923
    In the OnTriggerEnter, inside the !alreadyPlaying if block lower the movementspeed and in an OnTriggerExit put it back to the original value.

    Also it is a good idea if you make the movementSpeed private, add a public normalMovementSpeed and slowMovementSpeed and in the Start function make the movementSpeed equal to the normalMovementSpeed's value. Also when you make it slow and make it normal, use the slowMovementSpeed and normalMovementSpeed respectively. This way you can tweak these values from the inspector and you won't mess it up with hardcoded values in your code.

    ps: I think I go for a nap or something. Sorry, I completely misconstrued the entire situation here.
     
    Last edited: Apr 30, 2021
    Kurt-Dekker likes this.
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
  5. exiiplays91

    exiiplays91

    Joined:
    Mar 6, 2021
    Posts:
    2
    I think the PlayOneShot is there to make sure the audio doesn't play again after playing the first time. I'm incredibly new at this and the professor isn't super helpful so I'm just winging it here lol