footstep sound and velocities / sonidos de pasos y velocidades
Buenas! mi problema es el siguiente:
Tengo mi script de movimiento con sus respectivos sonidos, estos son: Correr, caminar, sprintar y apuntar. Hasta ahi bien, mi propblema llega cuando algunos de estos sonidos no se reproducen pero si que cambian sus respectivas velocidades de movimiento (apuntando llevas una velocidad, corriendo otra...), o no cambian las velocidades y solo algunos se oyen... y ya estoy desquiciado! A ver si alguien puede echarme una manilla!!! Un saludo! (no soy muy buen programador lo digo de antemano jeje)
Adjunto los dos scripts en cuestion
Movimiento con sonidos : using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerMovement : MonoBehaviour { public float WalkSpeed, Sprintspeed;
private CharacterController character_controller;
private Vector3 moveDirection;
public float speed = 4f;
private float gravity = 20f;
public float jumpForce = 10f;
private float verticalVelocity;
public bool IsWalking, IsSprinting, IsAiming;
public Animator animator;
public float Hor, Ver;
private PlayerFootsteps playerFootsteps;
private float sprint_volume = 1f;
private float crouch_volume = 0.1f;
private float walk_volume_min = 0.2f, walk_volume_Max = 0.6f;
private float walkStepDistance = 0.3f;
private float sprintStepDistance = 0.25f;
private float crouchStepDistance = 0.5f;
private void Awake()
{
playerFootsteps = GetComponentInChildren<PlayerFootsteps>();
character_controller = GetComponent<CharacterController>();
animator = GetComponentInChildren<Animator>();
}
// Start is called before the first frame update
void Start()
{
playerFootsteps.volumeMin = walk_volume_min;
playerFootsteps.VolumeMax = walk_volume_Max;
playerFootsteps.stepDistance = walkStepDistance;
}
// Update is called once per frame
void Update()
{
MoveThePlayer();
}
void LateUpdate()
{
speed = 4;
Apuntar();
Sprintar();
Caminar();
}
void MoveThePlayer()
{
float Hor = Input.GetAxis("Horizontal");
float Ver = Input.GetAxis("Vertical");
moveDirection = new Vector3(Hor, 0f, Ver);
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed * Time.deltaTime;
ApplyGravity();
character_controller.Move(moveDirection);
animator.SetFloat("VelocityRight", Hor);
animator.SetFloat("VelocityForward", Ver);
}
void ApplyGravity()
{
verticalVelocity -= gravity * Time.deltaTime;
PlayerJump();
moveDirection.y = verticalVelocity * Time.deltaTime;
}
void PlayerJump()
{
if(character_controller.isGrounded && Input.GetKeyDown(KeyCode.Space))
{
verticalVelocity = jumpForce;
}
}
void Apuntar()
{
if (Input.GetMouseButton(1))
{
IsAiming = true;
speed = WalkSpeed;
playerFootsteps.stepDistance = crouchStepDistance;
playerFootsteps.volumeMin = crouch_volume;
playerFootsteps.VolumeMax = crouch_volume;
animator.SetBool("IsAiming", true);
animator.SetBool("IsWalking", false);
animator.SetBool("IsSprinting", false);
}
else
{
IsAiming = false;
playerFootsteps.stepDistance = walkStepDistance;
playerFootsteps.volumeMin = walk_volume_min;
playerFootsteps.VolumeMax = walk_volume_Max;
animator.SetBool("IsAiming", false);
}
}
void Sprintar()
{
if (Input.GetKey(KeyCode.LeftShift) && IsAiming == false)
{
IsSprinting = true;
speed = Sprintspeed;
playerFootsteps.stepDistance = sprintStepDistance;
playerFootsteps.volumeMin = sprint_volume;
playerFootsteps.VolumeMax = sprint_volume;
animator.SetBool("IsSprinting", true);
}
else
{
IsSprinting = false;
playerFootsteps.stepDistance = walkStepDistance;
playerFootsteps.volumeMin = walk_volume_min;
playerFootsteps.VolumeMax = walk_volume_Max;
animator.SetBool("IsSprinting", false);
}
}
void Caminar()
{
if (Input.GetKey(KeyCode.LeftControl))
{
IsWalking = true;
speed = WalkSpeed;
playerFootsteps.stepDistance = crouchStepDistance;
playerFootsteps.volumeMin = crouch_volume;
playerFootsteps.VolumeMax = crouch_volume;
animator.SetBool("IsWalking", true);
}
else
{
IsWalking = false;
playerFootsteps.stepDistance = walkStepDistance;
playerFootsteps.volumeMin = walk_volume_min;
playerFootsteps.VolumeMax = walk_volume_Max;
animator.SetBool("IsWalking", false);
}
}
}
Script para añadir sonidos:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerFootsteps : MonoBehaviour {
private AudioSource footstepSound;
[SerializeField]
private AudioClip[] footstepClip;
private CharacterController characterController;
[HideInInspector]
public float volumeMin, VolumeMax;
private float accumulatedDistance;
[HideInInspector]
public float stepDistance;
// Start is called before the first frame update
void Awake()
{
footstepSound = GetComponent<AudioSource>();
characterController = GetComponentInParent<CharacterController>();
}
// Update is called once per frame
void Update()
{
CheckToPlayFootstepSound();
}
void CheckToPlayFootstepSound()
{
if (!characterController.isGrounded)
return;
if(characterController.velocity.sqrMagnitude > 0)
{
accumulatedDistance += Time.deltaTime;
if(accumulatedDistance > stepDistance)
{
footstepSound.volume = Random.Range(volumeMin, VolumeMax);
footstepSound.clip = footstepClip[Random.Range(0, footstepClip.Length)];
footstepSound.Play();
accumulatedDistance = 0f;
}
}
else
{
accumulatedDistance = 0f;
}
}
}
is almost imposible to know what exactly is wrong without being able to debug it, but is that code even working? i mean with that way of coding it the Esprintar and Apuntar method are usseles, it will never have playerFootsteps properties update for sprinting since you are overriding the properties each method (if you have an if else in ca$$anonymous$$ar the playerFootsteps value always will depend only of the Ca$$anonymous$$ar method), you should remove the ekse of each method and just create a default state. (making sure none of the other ifs were triggered). you need to change your question to english or it most likely will get removed