- Home /
Sound Effect Timing
im tryna make a sound play as megaman lands after jumping. this is my code, but the sound always plays immediately after he jumps, rather than when he hits the ground. what am i missing here?
private void FixedUpdate()
{
isGrounded = false;
RaycastHit2D raycastHit;
float raycastDistance = 0.05f;
int layerMask = 1 << LayerMask.NameToLayer("Ground");
Vector3 box_origin = box2d.bounds.center;
box_origin.y = box2d.bounds.min.y + (box2d.bounds.extents.y / 4f);
Vector3 box_size = box2d.bounds.size;
box_size.y = box2d.bounds.size.y / 4f;
raycastHit = Physics2D.BoxCast(box_origin, box_size, 0f, Vector2.down, raycastDistance, layerMask);
if (raycastHit.collider != null && !isJumping)
{
isGrounded = true;
}
else if (raycastHit.collider != null && isJumping)
{
isGrounded = true;
SoundManager.Instance.Play(MegaManLandClip);
isJumping = false;
}
}
void PlayerMovement()
if (keyJump && isGrounded)
{
isGrounded = false;
isJumping = true;
if (isShooting)
{
animator.Play("MegaManShoot+Jump");
}
else
{
animator.Play("MegaManJump");
}
rb2d.velocity = Vector2.up * jumpSpeed;
if (rb2d.velocity.y < 0)
{
rb2d.velocity += Vector2.up * Physics2D.gravity.y * fallMultiplier * Time.deltaTime;
}
else if (rb2d.velocity.y > 0 && !Input.GetKey(KeyCode.X))
{
rb2d.velocity += Vector2.up * Physics2D.gravity.y * lowJumpMultiplier * Time.deltaTime;
}
}
Answer by Nistroy · Jul 04, 2021 at 08:20 AM
Hi @Fuzzy_Puppy , instead of using a raycasthit (which you only use to check on you are on the ground), you could directly enter your isGrounded variable. You define it from groundCheckPosition that you will have to reference from the inspector with an empty GameObject which will be the player's child and position at his feet.
bool isGrounded;
bool isJumping;
public Transform groundCheck;
public float groundCheckRadius;
public LayerMask collisionLayers;
private void Update()
{
isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, collisionLayers);
if (isGrounded)
{
SoundManager.Instance.Play(MegaManLandClip);
isJumping = false;
}
}
private void OnDrawGizmos()
{
if (groundCheck != null)
{
Gizmos.DrawWireSphere(groundCheck.position, groundCheckRadius);
}
}
I would like to suggest doing this......to prevent the audio from playing in everyframe
instead of using if(isGrounded) only you should do
if (isGrounded && isJumping)
{
SoundManager.Instance.Play(MegaManLandClip);
isJumping = false;
}
There are obviously better solutions for making a nice trigger with strings or integers....but i won't recommend them unless necessary
Ah yes thin! I forgot that the sound is playing in an Update method. So I think the solution is maybe to use a boolean:
bool isGrounded;
bool isJumping;
bool playSong = false;
public Transform groundCheck;
public float groundCheckRadius;
public LayerMask collisionLayers;
private void Update()
{
isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, collisionLayers);
if (isGrounded && playSong)
{
SoundManager.Instance.Play(MegaManLandClip);
isJumping = false;
playSong = false;
}
if(Input.GetKeyDown(KeyCode.Space))
{
isJumping = true;
playSong = true;
}
}
private void OnDrawGizmos()
{
if (groundCheck != null)
{
Gizmos.DrawWireSphere(groundCheck.position, groundCheckRadius);
}
}
well no problem...i do some pretty stupid mistakes daily...it's just natural for us all to overlook something
Your answer
Follow this Question
Related Questions
Can't get Sound working but music is fine 1 Answer
AudioClip calculates length wrong 1 Answer
Sound plays right at the beginning 2 Answers
Can't play multiple sounds at once 2 Answers