Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
avatar image
0
Question by Navetelen · Apr 10, 2016 at 03:56 PM · animationscripting problem

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

}

Comment
Add comment
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

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.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

run animation when should idle 1 Answer

How to check in code when an animation is over? 1 Answer

Hi.How to change Spine Animation of a game object through a script that is Attached to another game object? 0 Answers

Changing Animator Controller Animations Via Script 3 Answers

Inputs and animations interfering with each other 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges