• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Dariocer · Apr 06, 2019 at 09:39 PM · velocitymovement scriptsounds

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;
     }
 }

}

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image xxmariofer · Apr 06, 2019 at 10:11 PM 0
Share

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 caminar the playerFootsteps value always will depend only of the Caminar 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

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

171 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Rotating, moving and keep on going with constant velocity in direction of click 1 Answer

Mouse controlled 2D object bleeding through walls 1 Answer

Character suddenly stop moving. 0 Answers

Rigidbody. Acceleration of free falling object works bad 0 Answers

Crossfading happens too fast 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges