unity3d – How do I get the joystick axis as a float in Unity 3D
I am trying to make audio play at the same speed as the player movement. Right now I have the audio playing as a float and the character moving with Unity’s character controller. How do I get the controller x and y axis as a float?
//Moving/Jumping
public CharacterController controller;
public float originalSpeed = 12;
public float speed = 12;
public float sprintSpeed = 20;
public float crouchSpeed = 5;
public float gravity = -50;
public float jumpHeight = 3f;
Vector3 velocity;
public float defaultSprintTime;
public float sprintTime;
private float x;
private float y;
In the update function:
//Getting the movement input
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
//Moving the player
Vector3 move = transform.right * x + transform.forward * z;
controller.Move(move * speed * Time.deltaTime);
In the audio coroutine:
if (playingStoneSound == false)
{
int randomSound = Random.Range(0, FootstepStone.Length); //Makes an int which randomly generates what audio source in the array should be played
FootstepStone[randomSound].Play(); //Plays audio from the array using the randomly generated int
playingStoneSound = true; //Making the bool true so you cannot play the sound again until it is done
if (stillCrouching == true) //If you are crouching play the sound slower
{
yield return new WaitForSeconds(0.85f); //Waits before playing the sound again
playingStoneSound = false; //Making the bool false so you can play the sound again
}
else //If you are not crouching
{
yield return new WaitForSeconds(speed); //Waits before playing the sound again -- This is where I want my audio to change depending on the joystick speed
playingStoneSound = false; //Making the bool false so you can play the sound again
}
}
Read more here: Source link