• 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 unity_FyE0Sdhh2I17RA · Mar 27, 2021 at 07:13 AM · controllerjumpjoystickbottom

player controller script jump buttom

hello I have a problem to implement the jump button in a 2d game I managed to route so that the horizontal movement with the joystick receives me but I cannot connect it with a jump button to be able to have the app on my cell phone: here is the player controller script:alt text

estoy tomando el script del de un juego 2d de unity : using System.Collections; using System.Collections.Generic; using UnityEngine; using Platformer.Gameplay; using static Platformer.Core.Simulation; using Platformer.Model; using Platformer.Core;

namespace Platformer.Mechanics {

 public class PlayerController : KinematicObject
 {
     public AudioClip jumpAudio;
     public AudioClip respawnAudio;
     public AudioClip ouchAudio;

     
     public float maxSpeed = 7;
     
     public float jumpTakeOffSpeed = 7;

     public JumpState jumpState = JumpState.Grounded;
     private bool stopJump;
    
     public Collider2D collider2d;
     
     public AudioSource audioSource;
     public Health health;
     public bool controlEnabled = true;

     bool jump;
     Vector2 move;
     SpriteRenderer spriteRenderer;
     internal Animator animator;
     readonly PlatformerModel model = Simulation.GetModel<PlatformerModel>();

     public Bounds Bounds => collider2d.bounds;

     void Awake()
     {
         health = GetComponent<Health>();
         audioSource = GetComponent<AudioSource>();
         collider2d = GetComponent<Collider2D>();
         spriteRenderer = GetComponent<SpriteRenderer>();
         animator = GetComponent<Animator>();
     }

     protected override void Update()
     {
         if (controlEnabled)
         {
             move.x = Input.GetAxis("Horizontal");
             if (jumpState == JumpState.Grounded && Input.GetButtonDown("Jump"))
                 jumpState = JumpState.PrepareToJump;
             else if (Input.GetButtonUp("Jump"))
             {
                 stopJump = true;
                 Schedule<PlayerStopJump>().player = this;
             }
         }
         else
         {
             move.x = 0;
         }
         UpdateJumpState();
         base.Update();
     }

     void UpdateJumpState()
     {
         jump = false;
         switch (jumpState)
         {
             case JumpState.PrepareToJump:
                 jumpState = JumpState.Jumping;
                 jump = true;
                 stopJump = false;
                 break;
             case JumpState.Jumping:
                 if (!IsGrounded)
                 {
                     Schedule<PlayerJumped>().player = this;
                     jumpState = JumpState.InFlight;
                 }
                 break;
             case JumpState.InFlight:
                 if (IsGrounded)
                 {
                     Schedule<PlayerLanded>().player = this;
                     jumpState = JumpState.Landed;
                 }
                 break;
             case JumpState.Landed:
                 jumpState = JumpState.Grounded;
                 break;
         }
     }

     protected override void ComputeVelocity()
     {
         if (jump && IsGrounded)
         {
             velocity.y = jumpTakeOffSpeed * model.jumpModifier;
             jump = false;
         }
         else if (stopJump)
         {
             stopJump = false;
             if (velocity.y > 0)
             {
                 velocity.y = velocity.y * model.jumpDeceleration;
             }
         }

         if (move.x > 0.01f)
             spriteRenderer.flipX = false;
         else if (move.x < -0.01f)
             spriteRenderer.flipX = true;

         animator.SetBool("grounded", IsGrounded);
         animator.SetFloat("velocityX", Mathf.Abs(velocity.x) / maxSpeed);

         targetVelocity = move * maxSpeed;
     }

     public enum JumpState
     {
         Grounded,
         PrepareToJump,
         Jumping,
         InFlight,
         Landed
     }
 }

}

captura-de-pantalla-2021-03-27-a-las-20939-am.png (138.7 kB)
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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

124 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

Related Questions

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

My Player's Jump and Gravity Speed is not the when how i want it to be 2 Answers

how can I make the character jump more by holding the jump button in this script? 1 Answer

2D top down projectile with joystick problem 1 Answer

How to use joystick.js script? it's not appearing in monodevelop. 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