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 Diamondville · Jul 14, 2017 at 12:45 PM · bugjumpinglooping

pls help my character have infinite jumping

using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement;

public delegate void DeadEventHandler();

public class Player : Character {

 private static Player instance;

 public event DeadEventHandler Dead;

 public static Player Instance
 {
     get
     {
         if (instance == null)
         {
             instance = GameObject.FindObjectOfType<Player>();
         }
         return instance;
     }
 }

 [SerializeField]
 private Transform[] groundPoints;

 [SerializeField]
 private float groundRadius;

 [SerializeField]
 private LayerMask whatIsGround;

 [SerializeField]
 private bool airControl;

 [SerializeField]
 private float jumpForce;

 private bool immortal = false;

 private SpriteRenderer spriteRenderer;

 [SerializeField]
 private float immortalTime;

 private float direction;

 private bool move;

 public Rigidbody2D myRigidbody { get; set; }

 public bool Jump { get; set; }

 public bool OnGround { get; set; }

 public override bool IsDead
 {
     get
     {
         if (health <= 0)
         {
             OnDead();
             {
                 SceneManager.LoadScene("GameOverScene");
             }
         }
       
         return health <= 0;
     }
 }

 private Vector2 startPos;



 // Use this for initialization
 public override void Start()
 {
    
     base.Start();

   
     startPos = transform.position;
     spriteRenderer = GetComponent<SpriteRenderer>();
     myRigidbody = GetComponent<Rigidbody2D>();
     
 }
 
 void Update()
 {
     if (!TakingDamage && !IsDead)
     {
         if (transform.position.y <= -14f)
         {
             Death();

         }
         HandleInput();
     }

 }

 // Update is called once per frame
 void FixedUpdate()
 {
     if (!TakingDamage)
     {
         float horizontal = Input.GetAxis("Horizontal");

         OnGround = IsGrounded();
         if (move)
         {
             HandleMovement(direction);
             Flip(direction);
         }
         else
         {
             HandleMovement(horizontal);

             Flip(horizontal);
         }
        
         HandleLayers();            
     }


 }
 
 public void OnDead()
 {
     if (Dead != null)
     {
         Dead();
     }
 }

 private void HandleMovement(float horizontal)
 {
     if (myRigidbody.velocity.y < 0)
     {
         MyAnimator.SetBool("land", true);
     }
     if (!Attack && (OnGround|| airControl))
     {
         myRigidbody.velocity = new Vector2(horizontal * movementSpeed, myRigidbody.velocity.y);
     }
     if (Jump && myRigidbody.velocity.y == 0)
     {
         myRigidbody.AddForce(new Vector2(0, jumpForce));
     }

     MyAnimator.SetFloat("speed", Mathf.Abs(horizontal));
 }
  
  
 private void HandleInput()
 {
     if (Input.GetKeyDown(KeyCode.W))
     {
         MyAnimator.SetTrigger("jump");
         Jump = true;
     }
     if (Input.GetKeyDown(KeyCode.LeftShift))
     {
         MyAnimator.SetTrigger("attack");
     }
 }  

 private void Flip(float horizontal)
 {
     if (horizontal > 0 && !facingRight || horizontal < 0 && facingRight)
     {
         ChangeDirection();
     }
 }

  
  private bool IsGrounded()
 {
     if (myRigidbody.velocity.y <= 0)
     {
         foreach (Transform point in groundPoints)
         {
             Collider2D[] colliders = Physics2D.OverlapCircleAll(point.position, groundRadius, whatIsGround);
             
             for (int i = 0; i < colliders.Length; i++)
             {
                 if (colliders[i].gameObject != gameObject)
                 {
                     return true; 
                 }
             }

         }

     }
     return false;
 }

 private void HandleLayers()
 {
     if (!OnGround)
     {
         MyAnimator.SetLayerWeight(1, 1);
     }
     else
     {
         MyAnimator.SetLayerWeight(1, 0);
     }

 }

 private IEnumerator IndicateImmortal()
 {
     while (immortal)
     {
         spriteRenderer.enabled = false;

         yield return new WaitForSeconds(.1f) ;

         spriteRenderer.enabled = true;

         yield return new WaitForSeconds(.1f);
     }
 }

 public override IEnumerator TakeDamage()
 {
     if (immortal)
     {

     }
     health -= 10;

     if (!IsDead)
     {
         MyAnimator.SetTrigger("damage");
         immortal = true;

         StartCoroutine(IndicateImmortal());
         yield return new WaitForSeconds(immortalTime);

         immortal = false;
     }
     else
     {
         MyAnimator.SetLayerWeight(1, 0);
         MyAnimator.SetTrigger("die");
     }
         
 }
 public void BtnJump()
 {
     MyAnimator.SetTrigger("jump");
     Jump = true;
     
 }

 public void BtnAttack()
 {
     MyAnimator.SetTrigger("attack");
 }

 public void BtnMove(float direction)
 {
     this.direction = direction;
     this.move = true;
 }

 public void BtnStopMove()
 {
     this.direction = 0;
     move = false;
 }

 public void BtnPaused()
 {
     MyAnimator.SetTrigger ("Paused");
 }

     

 public override void Death()
 {
     myRigidbody.velocity = Vector2.zero;
     MyAnimator.SetTrigger("idle");
     health = 30;
     transform.position = startPos;
     if (health <=0)
     {
         

     }
 }

 private void OnCollisionEnter2D(Collision2D other)
 {
     if(other.gameObject.tag == "Coin")
     {
         GameManager2.Instance.CollectedCoins++;
         Destroy(other.gameObject);
     }
 }

}

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

0 Replies

· Add your reply
  • Sort: 

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

113 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

Related Questions

While (starting jumping or in jump) holding W key makes game object jump high and fall slow. 1 Answer

A tiny sound bug I'm not sure how to solve. 1 Answer

3D Jumping Collision Bug 1 Answer

Is it possible to be on both the App thread and the UI thread? -1 Answers

Terrain editor always switches to paint hole mode,Terrain editor switches to paint holes 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