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 06, 2013 at 09:08 PM · playergravityjumprpg

Help with adding jump function to player.

I added or tried to add a jump function similar to how I added the move function. I also tried it the same way I added crouch/attack function. It doesn't do anything though he just sits still. Sometimes I can get it to work, but if I tap the button he barely moves up and the animation doesn't continue. I tried GetButtonUp but that doesn't work either.

Lines with asterisk symbols is what I tried to do to add jumping.

heres the code with jump function in it:

  public float speed = 1.0f;
 *****public float jumpspeed = 1.0f;******
 
  
  
     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;
 ************bool jumping = false;*************
     
     *********if(Input.GetKey(KeyCode.Space)){
              moveVector.y += 2.0f;
             jumping = false;
         }*************
         
  
  
  
     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;
  }
     
   ************private void Jump(Vector3 moveVector) {
        FlipSprite(moveVector.x);
        moveVector *= jumpspeed * Time.deltaTime;
        thisTransform.position = thisTransform.position + moveVector;
        sprite.Play("jump");****************
  
  
  
  
     }
  
  
  
  
  
 }
Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by getyour411 · Sep 06, 2013 at 09:19 PM

I don't see you calling Jump() anywhere. Somewhere tween 45 - 65 you are probably falling through to Stand(), Google 'fence the wolf' and use some print/debugs to see where your code is failing.

Comment
Add comment · Show 1 · 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 07, 2013 at 12:15 AM 0
Share

I tried what you said and called Jump(),but it still doesn't work. It gives me the error that Jump calls for 0 arguments. I tried it a different way that allowed me to call jump, but that made my walking animation not work, I couldn't attack and I couldn't crouch.

This is the newer script:

 using UnityEngine;
 using System.Collections;
 using SpriteFactory;
  
 public class player : $$anonymous$$onoBehaviour {
  
  
  
   public float speed = 1.0f;
 public float jumpspeed = 1.0f;
  
  
  
     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;
 
  
   if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.Space)){
           Jump();
        } else {
          if(!IsJumping()) {
           if(moving) {
               $$anonymous$$ove(moveVector);
           } else {
               Stand();
          }
        }
  
  
  
  
     if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.A)){
          moveVector.x -= 1.0f;
          moving = true;
  
     }
  
        if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.D)){
          moveVector.x += 1.0f;
          moving = true;
  
  
        }
  
  
        if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.$$anonymous$$ouse0)){
          Attack();
        } else {
          if(!IsAttacking()) {
           if(moving) {
               $$anonymous$$ove(moveVector);
           } else {
               Stand();
          }
        }
  
        if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.S)){
          Crouch();
        } else {
          if(!IsCrouching()) {
  
          }
            if(moving) {
               $$anonymous$$ove(moveVector);
           } 
           else {
          Stand();
     }
     }
        }
     }
     }
  
  
  
  
  
  
     private void Stand() {
        sprite.Stop();
     }
  
  
     private void $$anonymous$$ove(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 = $$anonymous$$athf.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() {
       
     }
  
     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;
  }
  
   private void Jump() {
       if(IsJumping()) return;
  
        sprite.Play("jump");
  
  
  
     }
     
     private bool IsJumping() {
         if(sprite.IsAnimationPlaying("jump")) {return false;}
        return false;
     }
 }
avatar image
0

Answer by nasby321 · Sep 10, 2013 at 02:44 AM

I fixed it myself heres the new script if any one wants to see it:

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 · 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

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

Gravity and Jumping for a Character Controller 1 Answer

Adding a jump feature help? 2 Answers

Particular "Jump" Script. 2 Answers

Jumping script WONT WORK!!!!!!!! for 2D 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