- Home /
Question by
MATCAVERNA · Mar 03, 2019 at 10:14 PM ·
animation2d gameanimatorcollider2dcharacter movement
how to make a irregular ground or slope,About irregular terrains and slopes
Hi, english is not my first language but, well Im trying to make a little curve stone bridge on my 2d project and used a polygon collider on it to follow the shape but when my character walks through the bridge the animator says that the character is jumping and doesn't show the running animation
here is a video of it
Comment
Nice start would be to know how you define "jumping" in your code
public float maxSpeed;
public float jumpForce = 10.0f;
public Transform groundChecker;
private bool grounded = true;
private bool jumping;
private Rigidbody2D rb2d;
private Animator anim;
private SpriteRenderer sprite;
void Awake ()
{
rb2d = GetComponent<Rigidbody2D> ();
sprite = GetComponent<SpriteRenderer> ();
anim = GetComponent<Animator> ();
gameObject.AddComponent<PolygonCollider2D>();
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
grounded = Physics2D.Linecast (transform.position, groundChecker.position, 1 << Layer$$anonymous$$ask.NameToLayer ("ground"));
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space) && grounded)
{
jumping = true;
}
}
void OnCollisionEnter2D(Collision2D coll)
{
if (coll.gameObject.tag == "ground")
{
grounded = true;
}
}
void FixedUpdate(){
float move = Input.GetAxis("Horizontal");
anim.SetFloat("Speed", $$anonymous$$athf.Abs(move));
rb2d.velocity = new Vector2(move * maxSpeed, rb2d.velocity.y);
if((move> 0f && sprite.flipX) || (move < 0f && !sprite.flipX))
{
Flip();
}
if (jumping)
{
rb2d.AddForce(new Vector2(1f, jumpForce));
jumping = false;
}
anim.SetBool("jumpFall", rb2d.velocity.y != 0f);
}
void Flip()
{
sprite.flipX = !sprite.flipX;
}
}
Ok, your condition for "jumpFall" is that your vertical velocity is different from 0. When you are walking on your bridge, it's obviously not a zero because you are moving up and down.
For now you could add rb2d.velocity.y != 0f && !grounded