I’ve already added a lot of sound effects in this and other scripts, but I’m not able to figure out how to add a landing sound effect when the player lands on one of the platforms.
(Note: I’ve already added a variable ‘landingSound’.)
This is the current script:
public float moveSpeed;
private float moveSpeedSafe;
public float moveSpeedMultiplier;
public float speedIncreasePoint;
private float speedIncreacePointSafe;
public float speedPointMultiplier;
private float speedPointCount;
private float speedPointCountSafe;
public float jumpForce;
public float jumpTime;
private float jumpTimeCounter;
private bool stoppedJumping;
private Rigidbody2D myRigidbody;
private bool onPlatform;
public LayerMask whatIsPlatform;
public Transform platformChecker;
public float platformCheckRadius;
/*private Collider2D myCollider;*/
private Animator myAnimator;
public gameManager theGameManager;
public AudioSource jumpSound;
public AudioSource deathSound;
public AudioSource landingSound;
void Start()
{
myRigidbody = GetComponent<Rigidbody2D>();
/*myCollider = GetComponent<Collider2D>();*/
myAnimator = GetComponent<Animator>();
jumpTimeCounter = jumpTime;
speedPointCount = speedIncreasePoint;
moveSpeedSafe = moveSpeed;
speedPointCountSafe = speedPointCount;
speedIncreacePointSafe = speedIncreasePoint;
stoppedJumping = true;
}
void Update()
{
/*onPlatform = Physics2D.IsTouchingLayers(myCollider, whatIsPlatform);*/
onPlatform = Physics2D.OverlapCircle(platformChecker.position, platformCheckRadius, whatIsPlatform);
if(transform.position.x > speedPointCount)
{
speedPointCount += speedIncreasePoint;
speedIncreasePoint = speedIncreasePoint * speedPointMultiplier;
moveSpeed = moveSpeed * moveSpeedMultiplier;
}
myRigidbody.velocity = new Vector2(moveSpeed, myRigidbody.velocity.y);
if (Input.GetKeyDown(KeyCode.Space))
{
if(onPlatform)
{
myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, jumpForce);
stoppedJumping = false;
jumpSound.Play();
}
}
if (Input.GetKey(KeyCode.Space) && !stoppedJumping)
{
if(jumpTimeCounter > 0)
{
myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, jumpForce);
jumpTimeCounter -= Time.deltaTime;
}
}
if (Input.GetKeyUp(KeyCode.Space))
{
jumpTimeCounter = 0;
stoppedJumping = true;
}
if (onPlatform)
{
jumpTimeCounter = jumpTime;
}
myAnimator.SetFloat("Speed", myRigidbody.velocity.x);
myAnimator.SetBool("onPlatform", onPlatform);
}
void OnCollisionEnter2D(Collision2D other)
{
if(other.gameObject.tag == "deathFloor")
{
theGameManager.RestartGame();
moveSpeed = moveSpeedSafe;
speedPointCount = speedPointCountSafe;
speedIncreasePoint = speedIncreacePointSafe;
deathSound.Play();
}
}