Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by nasby321 · Sep 10, 2013 at 02:43 AM · animationplayerjumpside-scrolling

How do I get my jump animation to work?

I finally got jumping in my game, but now I want him to switch to his jump animation when he is not grounded. I tried putting it with the void jump and within the void OnCollisionEnter, as well as under the jump button in Update, but it does not play.

 using UnityEngine;
 using System.Collections;
 using SpriteFactory;
  
 public class player : MonoBehaviour {
  
  
  
     public float speed = 1.0f;
     public float jumpSpeed = 10.0f;
     public float gravity = 20.0f;
     public bool grounded = true;
  
  
     private Transform thisTransform;
     private Sprite sprite;
     private bool flipped;
  
     // Use this for initialization
     void Awake () {
        thisTransform = transform;
        sprite = (Sprite)GetComponent(typeof(Sprite));
  
     }
  
     // Update is called once per frame
     void Update () {
        Vector3 moveVector = new Vector3(0,0,0);
        bool moving = false;
         
         moveVector.y -= gravity * Time.deltaTime;
         
         if(Input.GetKeyDown(KeyCode.Space)){
              Jump();
             
             
             
         }
     
         
  
  
  
     if(Input.GetKey(KeyCode.A)){
          moveVector.x -= 1.0f;
          moving = true;
  
     }
  
        if(Input.GetKey(KeyCode.D)){
          moveVector.x += 1.0f;
          moving = true;
  
  
        }
  
  
        if(Input.GetKey(KeyCode.Mouse0)){
          Attack();
        } else {
          if(!IsAttacking()) {
           if(moving) {
               Move(moveVector);
           } else {
               Stand();
          }
        }
        
        if(Input.GetKey(KeyCode.S)){
          Crouch();
        } else {
          if(!IsCrouching()) {
                     
             }
                  if(moving) {
               Move(moveVector);
           } 
                 else {
          Stand();
     }
     }
        }
     }
 
  
  
  
  
  
     private void Stand() {
        sprite.Stop();
     }
  
  
     private void Move(Vector3 moveVector) {
        FlipSprite(moveVector.x);
        moveVector *= speed * Time.deltaTime;
        thisTransform.position = thisTransform.position + moveVector;
        sprite.Play("walk");
  
  
  
  
     }
  
     private void FlipSprite(float x) {
        if(x == 0.0f) return;
  
        float xDir = Mathf.Sign(x);
        if(!flipped && xDir < 0.0f) {
          flipped = true;
          sprite.SetFlippedState(true, false);
        } else if(flipped&& xDir > 0.0f) {
          flipped = false;
              sprite.SetFlippedState(false, false);
        }
     }
  
     private void Attack() {
        if(IsAttacking()) return;
  
        sprite.Play("Sword");
     }
  
     private bool IsAttacking() {
        if(sprite.IsAnimationPlaying("Sword")){ return false;}
        return false;
  
  
 }
  
     private void Crouch() {
        if(IsCrouching()) return;
  
        sprite.Play("crouch");
     }
  
     private bool IsCrouching() {
        if(sprite.IsAnimationPlaying("crouch")) {return false;}
        return false;
  
  
  
  
  
  
  
  
 }  
     void Jump(){
         if(grounded == true)
         {
         rigidbody.AddForce(Vector3.up * jumpSpeed);
             grounded = false;
     }
     }
     
     void OnCollisionEnter(Collision col){
         if(col.gameObject.tag == "floor"){
             grounded = true;
             sprite.Play("jump");
         }
             
         
         }
  
  
  
  
  
 }
Comment
Add comment · Show 3
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 getyour411 · Sep 10, 2013 at 04:25 AM 0
Share

Add

 Debug.Log("Now standing.");

to your Stand() function and see if that immediately occurs after your jump. I think your placement of the sprite.Play("jump") in the OnCollision might be getting immediately overwritten by the code falling through to Stand() and sprite.Stop(). Is there a reason why you don't have sprite.Play("jump") in Jump()?

avatar image nasby321 · Sep 10, 2013 at 04:02 PM 0
Share

Like in my comment I have tried placing it in the void called jump. Here is what I have tried:

 if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space)){
           Jump();
          sprite.Play("jump");
 
 void Jump(){
        if(grounded == true)
        {
        rigidbody.AddForce(Vector3.up * jumpSpeed);
          grounded = false;
         sprite.Play("jump");
     }
     }
 Also Tried this:
 
 void Jump(){
        if(grounded == true)
        {
        rigidbody.AddForce(Vector3.up * jumpSpeed);
          grounded = false;
  
     }
       if(grounded == false){
      sprite.Play("jump");
 }
     }
 
 
avatar image fafase · Sep 12, 2013 at 06:21 AM 0
Share

There is a problem with how you check if you are grounded. Using OnCollisionEnter means that any collision will make the guy grounded. Suppose you jump and hit with the head, you are grounded.

If you want to make sure you are hitting with the feet, you can use the capsule and check which part has hit or a raycast downward.

I see that you are checking if it is ground but I bet a platform placed above that you are supposed to access is also tagged as ground.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by fafase · Sep 10, 2013 at 04:12 PM

Add a raycast that goes from the center of the guy towards the ground at a distance of half the guy's height. Then if the raycast is false it means you are in the air, play jump animation.

 Transform tr;
 float height;
 int layerMask;

 void Start(){
    tr = transform;
    height = collider.bounds.extent.y;
    layerMask = LayerMask.NameToLayer("Player");
 }
 void Update(){
    if(!Physics.Raycast(tr.position, Vector3.down, height,layerMask)){
       anim.CrossFade("Jump");
    }
 }

Best would also be to check if you are jumping or falling since you may not play the same animation for each. The layer allows the cast to go through the player or ignore the player.

Comment
Add comment · Show 3 · 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 nasby321 · Sep 10, 2013 at 05:18 PM 0
Share

This stops the walking animation in the air, but doesn't play animation. I have use the sprite.Play from sprite factory for an animation on my character to play. I should have said this at the beginning. I'm making a 2D side-scroller with Sprite Factory which I have to put Sprite.(Function/Call/whateverelse) to get animations to play.

I'm fine with it stopping the walking animation in the air, but now with what I added caused my attack part of the script to continuously hurt the enemy ins$$anonymous$$d of having to hit the button every time to deal damage.

avatar image nasby321 · Sep 10, 2013 at 05:22 PM 0
Share

Also you forgot the "s" at the end of "extents".

avatar image fafase · Sep 10, 2013 at 05:35 PM 0
Share

Fact is I do not know how you use this sprite thing. But since the walk anim stops it is just about triggering the jump anim when in the air. And yes s is missing.

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

15 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

how do I script this jump animation? 1 Answer

jumping and landing workflow 0 Answers

Help with adding jump function to player. 2 Answers

How To Make Enemy Chase You? 1 Answer


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