Hello Guest

Author Topic: Grounded Linecast issues with animation  (Read 4842 times)

singh029

  • Newbie
  • *
  • Posts: 5
    • View Profile
Grounded Linecast issues with animation
« on: April 11, 2014, 06:32:12 am »
Hi im having some trouble playing my walk animation when the player is grounded.

When the player is not grounded, the jump animation should play, which works fine.
But when the player hits the ground, he just stops and stays in jump animation. The player should automatically start walking!
Please help!

I have a groundCheck game object set underneath the player
And jump animation is just one sprite looped.

Code: [Select]
tk2dSpriteAnimator animator;
private Transform groundCheck; // A position marking where to check if the player is grounded.
private bool grounded = false; //Whether or not the player is grounded.

// Use this for initialization
void Start ()
{
animator = GetComponent<tk2dSpriteAnimator> ();
groundCheck = transform.Find("groundCheck");

Invoke ("PlayInABit", 5f);
}

// Update is called once per frame
void Update ()
{
grounded = Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Layer 0")); 

if(animator.IsPlaying("Walk") && animator.CurrentFrame >= 1)
{
transform.Translate (1.5f * Time.deltaTime, 0, 0);
}

if(grounded)
{
Debug.Log (grounded.ToString());
animator.Play ("Walk");
}
else
{
Debug.Log (grounded.ToString());
animator.Play ("Jump");
}
}

void PlayInABit()
{
animator.Play ("Walk");
}

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Grounded Linecast issues with animation
« Reply #1 on: April 11, 2014, 02:19:54 pm »
Is it printing out the expiated messages? There doesn't seem to be any complicated code there. Unless its grounded for one frame and no more after that?

singh029

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Grounded Linecast issues with animation
« Reply #2 on: April 11, 2014, 02:51:16 pm »
yeah man, only the second log message. it always returns false. Both of the animations are set to loop. The player has a rigidbody attached and a box collider. And the tilemap ground has an edge collider attached when play is hit.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Grounded Linecast issues with animation
« Reply #3 on: April 11, 2014, 03:59:16 pm »
The line cast probably isn't returning true because the collider on the tile map is an edge collider and is just a "skin". Use OverlapCircle with a small radius instead to get some buffer for intersection errors.

singh029

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Grounded Linecast issues with animation
« Reply #4 on: April 14, 2014, 11:50:40 pm »
ok i fixed it. i didnt have the layers set up correctly. I think the player player layer was the same as the ground layer. i looked in unity's 2d example and fixed it.

Code: [Select]
grounded = Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground"));