- Home /
Controlling animation with IsTouchingLayers doesn't work on moving platform.
In my 2d platformer game I used IsTouchingLayers to check if the Player is on the ground. I made it according to a video tutorial series on youtube and it works like a wonder. It is always running when on ground, and after jumping, it loops the middle of jumping animation until it reaches the "Ground" layer again. It is perfectly fine, but on a moving platform it doesn't work. I trigger an invisible object to move that platform and the platform moves with the Player. (The Player is running at a constant speed to the right). The weird thing is that it triggers the grounded status on and off every half second while on the moving platform. So I can still jump, but the animation is bouncing between running and jumping really fast. I know I might have done it ineffectively but I just don't have the experience to fully understand what should I change. I tried various methods but things just got worse or simply didn't work.
Here's the PlayerController script on my Player:
using UnityEngine; using System.Collections;
public class PlayerController : MonoBehaviour {
public float moveSpeed;
public float jumpForce;
private Rigidbody2D myRigidbody;
public bool grounded;
public LayerMask whatIsGround;
private Collider2D myCollider;
private Animator myAnimator;
private bool canDoubleJump;
public AudioClip deathSound;
public AudioClip jumpSound;
public int Health;
public SoundManager SM;
void Start () {
Health = PlayerPrefs.GetInt("Health");
myRigidbody = GetComponent<Rigidbody2D>();
myCollider = GetComponent<Collider2D>();
myAnimator = GetComponent<Animator>();
}
void Update () {
//if we touch the "Ground" Layer,grounded is true
grounded = Physics2D.IsTouchingLayers(myCollider,whatIsGround);
if (grounded)
{
canDoubleJump = true;
}
//running
myRigidbody.velocity = new Vector2(moveSpeed, myRigidbody.velocity.y);
//die if Player falls too low
if (transform.position.y < -20)
{
if (Health > 1)
{
Health--;
PlayerPrefs.SetInt("Health", Health);
Application.LoadLevel(Application.loadedLevel);
}
else
{
Application.LoadLevel(0);
}
}
//if grounded is true in Animator it is automatically playing the running animation
myAnimator.SetBool("Grounded", grounded);
}
public void jump()
{
if (grounded)
{
myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, jumpForce);
SM.PlaySingle(jumpSound);
}
if (!grounded && canDoubleJump)
{
myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, jumpForce);
canDoubleJump = false;
SM.PlaySingle(jumpSound);
}
}
void OnCollisionEnter2D(Collision2D other)
{
//die when touching a trap
if(other.gameObject.tag == "trap")
{
StartCoroutine(Die());
}
}
IEnumerator Die()
{
myAnimator.SetBool("Dead", true);
SM.PlaySingle(deathSound);
yield return new WaitForSeconds(0.35f);
if (Health > 1)
{
Health--;
PlayerPrefs.SetInt("Health", Health);
Application.LoadLevel(Application.loadedLevel);
}
else
{
Application.LoadLevel(0);
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612074545im_/https://answers.unity.com/themes/thub/images/avi.jpg)