- Home /
Unity 2D Enemy Frog Jumping Issue It Keeps On Getting Lower & Lower???
From the start of the game, when i start it up & run it, the frog will start jumping. After it lands from jumping it will get lower & lower, like each time it jumps it pushes the ground it was on, before jumping, down by 0.2 & then lands on the new lower ground. If anyone knows why or could help me figure out why its doing this each time it jumps i would very much appreciate it. Thank You. O & here are some pics to understand what im talking about and my code for it.
pic 1 is beginning before first jump
pic 2 is now after about 15-20 jumps (3 left then 3 right)
now the script code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FrogScript : MonoBehaviour {
private Animator anim;
private bool animation_Started;
private bool animation_Finished;
private int jumpedTimes;
private bool jumpLeft = true;
private string coroutine_Name = "FrogJump";
public LayerMask playerLayer;
private GameObject player;
void Awake()
{
anim = GetComponent<Animator>();
}
// Use this for initialization
void Start () {
StartCoroutine(coroutine_Name);
player = GameObject.FindGameObjectWithTag(MyTags.PLAYER_TAG);
}
void Update()
{
if (Physics2D.OverlapCircle(transform.position, 0.5f, playerLayer))
{
player.GetComponent<PlayerDamage>().DealDamage();
}
}
void LateUpdate () {
if (animation_Finished && animation_Started)
{
animation_Started = false;
transform.parent.position = transform.position;
transform.localPosition = Vector3.zero;
}
}
IEnumerator FrogJump()
{
yield return new WaitForSeconds(Random.Range(1f, 4f));
animation_Started = true;
animation_Finished = false;
jumpedTimes++;
if (jumpLeft)
{
anim.Play("FrogJumpLeft");
} else {
anim.Play("FrogJumpRight");
}
StartCoroutine(coroutine_Name);
}
void AnimationFinished()
{
animation_Finished = true;
if (jumpLeft)
{
anim.Play("FrogIdleLeft");
} else
{
anim.Play("FrogIdleRight");
}
if (jumpedTimes == 3)
{
jumpedTimes = 0;
Vector3 tempScale = transform.localScale;
tempScale.x *= -1;
transform.localScale = tempScale;
jumpLeft = !jumpLeft;
}
}
}//class
I am just a noob game dev but, my guess is your jump animation must be disabling or changing the scaling of the collider. ie; when the frog jumps its collider turns off and when it reaches the ground it activates again. But, if there is delay in activation it reaches into the ground and so... Will be glad to know if it helps.
Answer by Magso · Mar 31, 2020 at 12:23 PM
transform.parent.position = transform.position;
will be the problem if the last position.y key of the animation is offset, there's nothing else in your code that would cause it to sink down like that.
Also a tip for the coroutine, you're using StartCoroutine(coroutine_Name)
to create a loop, instead use a while loop like this.
IEnumerator FrogJump()
{
while(true)
{
yield return new WaitForSeconds(Random.Range(1f, 4f));
animation_Started = true;
animation_Finished = false;
jumpedTimes++;
if (jumpLeft)
{
anim.Play("FrogJumpLeft");
} else {
anim.Play("FrogJumpRight");
}
}
}