Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 managerofSAW · Aug 13, 2015 at 09:02 AM · touchgravitychar

Can't use more than 1 button and falls slow

How would I make it so that you could run and jump? Also when falling and trying to use air control the character would not fall at their original pace. I'm new here so an explanation would be great.

using UnityEngine; using System.Collections; public class PlatformerCharacter2D : MonoBehaviour {

 public float maxSpeed = 15f;
 bool facingRight = true;
 
 Animator anim;
 
 //sets up the grounded stuff
 bool grounded = false;
 bool touchingWall = false; 
 public Transform groundCheck;
 public Transform wallCheck;
 float groundRadius = 0.2f;
 float wallTouchRadius = 0.2f;
 public LayerMask whatIsGround;
 public LayerMask whatIsWall;
 public float jumpForce = 500f;
 public float jumpPushForce = 2f;

 public GUITexture guiLeft;
 public GUITexture guiRight;
 public GUITexture guiJump;
 
 bool moveLeft, moveRight, doJump = false;
 public float moveSpeed = 5f;
 float move = 0;
 
 //double jump
 bool doubleJump = false;
 
 // Use this for initialization
 void Start () {
     
     anim = GetComponent<Animator>();
 }
 
 // Update is called once per frame
 void FixedUpdate () {

     if (moveLeft)
     {
         rigidbody2D.velocity = -Vector2.right * moveSpeed;
     }
     
     if (moveRight)
     {
         rigidbody2D.velocity = Vector2.right * moveSpeed;
     }
     
     // The player is grounded if a linecast to the groundcheck position hits anything on the ground layer.
     grounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);
     touchingWall = Physics2D.OverlapCircle(wallCheck.position, wallTouchRadius, whatIsWall);
     anim.SetBool("Ground", grounded);
     
     if (grounded) 
     {
         doubleJump = false;
     }
     
     if (touchingWall) 
     {
         grounded = false; 
         doubleJump = false; 
     }
     
     anim.SetFloat ("vSpeed", rigidbody2D.velocity.y);
     
     

     anim.SetFloat("Speed", Mathf.Abs (move));
     
     rigidbody2D.velocity = new Vector2(move * maxSpeed, rigidbody2D.velocity.y);
     
     // If the input is moving the player right and the player is facing left...
     if(move > 0 &&!facingRight){
         // ... flip the player.
         Flip ();
     }// Otherwise if the input is moving the player left and the player is facing right...
     else if(move < 0 && facingRight){
         // ... flip the player.
         Flip ();
     }
 }
 void Update()
 {
     if (Input.touchCount > 0)
     {
         // Get the touch info
         Touch t = Input.GetTouch(0);
         
         // Did the touch action just begin?
         if (t.phase == TouchPhase.Began)
         {
             if (guiLeft.HitTest(t.position, Camera.main))
             {
                 moveLeft = true;
                 move = -1;
                 if((grounded) && (guiJump.HitTest(t.position, Camera.main)))
                 {
                     anim.SetBool("Ground", false);
                     rigidbody2D.AddForce(new Vector2(0, jumpForce));
                     
                     if(!grounded)
                     {
                         doubleJump = false;
                     }
                 }
                 
                 if (touchingWall && (guiJump.HitTest(t.position, Camera.main)))
                 {
                     WallJump ();
                 }
             }
             
             // Are we touching the right arrow?
             if (guiRight.HitTest(t.position, Camera.main))
             {
                 moveRight = true;
                 move = 1;
                 if((grounded) && (guiJump.HitTest(t.position, Camera.main)))
                 {
                     anim.SetBool("Ground", false);
                     rigidbody2D.AddForce(new Vector2(0, jumpForce));
                     
                     if(!grounded)
                     {
                         doubleJump = false;
                     }
                 }
                 
                 if (touchingWall && (guiJump.HitTest(t.position, Camera.main)))
                 {
                     WallJump ();
                 }
             }
     // If the jump button is pressed and the player is grounded then the player should jump.
             if((grounded) && (guiJump.HitTest(t.position, Camera.main)))
             {
                 anim.SetBool("Ground", false);
                 rigidbody2D.AddForce(new Vector2(0, jumpForce));
         
                 if(!grounded)
                 {
                     doubleJump = false;
                 }
             }
     
             if (touchingWall && (guiJump.HitTest(t.position, Camera.main)))
             {
                 WallJump ();
             }
         }
         if (t.phase == TouchPhase.Ended)
         {
             Debug.Log("hajdhfjadsklj");
             // Stop all movement
             moveLeft = false;
             moveRight = false;
             rigidbody2D.velocity = Vector2.zero;
             move = 0;
         }
     }
 }

 void WallJump () 
 {
     rigidbody2D.AddForce (new Vector2 (jumpPushForce, jumpForce));
 }
 
 
 void Flip()
 {
     
     // Switch the way the player is labelled as facing
     facingRight = !facingRight;
     
     //Multiply the player's x local cale by -1
     Vector3 theScale = transform.localScale;
     theScale.x *= -1;
     transform.localScale = theScale;
 }

}

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

2 People are following this question.

avatar image avatar image

Related Questions

Gravity Switch Touch 1 Answer

A touch'es state is always Began and the position doesn't change 1 Answer

Bike wobbles like pendulum after applying gravity 1 Answer

Scaling giant images as 2D Sprite 0 Answers

Js: UnityEngine.Rigidbody is required, how to fix? 2 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