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 /
  • Help Room /
avatar image
0
Question by Paratrooper82 · Jan 30, 2018 at 02:57 AM · scripting problemunity 2djumpjumpingjumping object

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: alt text

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

jumping-frog.png (105.4 kB)
Comment
Add comment · Show 1
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
avatar image Paratrooper82 · Jan 30, 2018 at 03:30 AM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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

Comment
Add comment · Show 2 · Share
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
avatar image Paratrooper82 · Jan 30, 2018 at 07:19 PM 0
Share

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.

avatar image PersianKiller Paratrooper82 · Jan 31, 2018 at 05:00 AM 0
Share

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

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

180 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Jump buffering and Coyote time causes double jump when spamming jump 2 Answers

Jump Force/Velocity is Wrong and Strange. Help 1 Answer

Making an object jump (without rigidbodies), while using enum + gravity 0 Answers

how to stop infinite jumping in air? 0 Answers

Can't jump/buggy when standing next to an object. 0 Answers


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