Unity 2D Enemy jump Question (Frog jump)
Hey, I think I need a little help with this, as I can't find an answer anywhere and I've tried a lot so far.
Issue: I have a frog enemy, that is "supposed to jump" and also play the jumping animation (I have both sprites Idle and Jump). For the love of me I can't get a proper jumping frog script together, and neither can I figure out how to play the jump animation at the right time.
Picture of frog in air:
Here is the code I have for the frog so far, but I just feel like making it continuously jump on a "Ground trigger" is horrible. This is my frog script so far.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FrogController : MonoBehaviour {
public float moveSpeed;
private bool canMove;
public GameObject deathFrogExplosion;
private Rigidbody2D myRigidBody;
//new with radius
public Transform playerCheck;
public float playerCheckRadius;
public LayerMask whatIsPlayer;
// Use this for initialization
void Start () {
myRigidBody = GetComponent<Rigidbody2D> ();
}
// Update is called once per frame
void Update () {
canMove = Physics2D.OverlapCircle(playerCheck.position, playerCheckRadius, whatIsPlayer);
if (canMove) {
myRigidBody.velocity = new Vector3 (-moveSpeed, myRigidBody.velocity.y, 0f);
}
}
void OnTriggerEnter2D(Collider2D other){
if (other.tag == "KillPlane") {
//Destroy (gameObject);
gameObject.SetActive (false);
Instantiate (deathFrogExplosion, other.transform.position, other.transform.rotation);
}
if (other.tag == "Water") {
//Destroy (gameObject);
gameObject.SetActive (false);
Instantiate (deathFrogExplosion, other.transform.position, other.transform.rotation);
}
//have object move opposite way when tag "wall"
if (other.tag == "Wall") {
moveSpeed *= -1;
}
if (other.tag == "Ground") {
myRigidBody.velocity = new Vector3 (myRigidBody.velocity.x, 4, 0f);
}
}
void OnEnable(){
canMove = false;
}
}
I figured out the proper rotation with this:
if (moveSpeed < 0) {
transform.localScale = new Vector3 (-1f, 1f, 1f);
} else {
transform.localScale = new Vector3 (1f, 1f, 1f);
}
But I still can't seem to figure out how to make a well working jump script. Like maybe have the frog jump, then wait a second, and jump again. But play the animation only when the frog jumps.
Answer by PersianKiller · Jan 30, 2018 at 04:48 PM
public Animator anim;
void Start () {
anim = GetComponent<Animator> ();
}
void Update(){
if(jump){
anim.play("JumpAnimationName");
}
else if(move){
anim.play("MoveAnimationName");
}
}
it's an example :) if you need more help leave a comment I'll help If I could
Hey, thanks! I am playing around with it now. Do you by chance know a better jump function for my frog? Right now I have a "void OnTriggerEnter2D (Collider2D other)" function that says:
if (other.tag == "Ground") {
myRigidBody.velocity = new Vector3 (myRigidBody.velocity.x, 4, 0f);
}
However this seems to make the frog jump every single time it hits the ground. But I can't seem to tie your code towards this.
hi,you mean you want when it jumps , then jump animation should be played???
watch this tutorial,it might be helpful.I don't know what you want :) more explain please.
(it's about how to create and use animations) https://streamable.com/9bxup