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 Lankybean · Mar 02, 2015 at 04:35 PM · c#2dmovementsprite

Question on Sprite and Movement

Hi, Im making a 2d platform game, im having an issue! Im looking to add sprites but Im not sure how to go about it (Code below) also theres a jumping problem when I press up and right the player moves in that direction, but when the player tries Up and Left they jump right? if anyone could help with either of the problem that would be awesome! I've posted the code below. Thanks! Joe

 using UnityEngine;
 using System.Collections;
 
 public class PlayerInput : MonoBehaviour {
 
     public float MaxSpeed = 5;
     public float Acceleration = 10;
     public float JumpSpeed = 6;
     public float JumpDuration;
 
     public bool EnableDoubleJump = true;
     public bool wallHitDoubleJumpOverride = true;
 
     bool canDoubleJump = true;
     float jmpDuration;
     bool jumpKeyDown = false;
     bool canVariableJump = false;
 
 
 
 
     void Update () 
     {
         float horizontal = Input.GetAxis ("Horizontal");
 
         if (horizontal < -0.1f) 
         {
                 if (rigidbody2D.velocity.x > -this.MaxSpeed) 
                     {
                         rigidbody2D.AddForce (new Vector2(-this.Acceleration, 0.0f));
                     } 
                 else 
                     {
                         rigidbody2D.velocity = new Vector2(-this.MaxSpeed, rigidbody2D.velocity.x);
                     }
 
         } 
         else if (horizontal > 0.1f) 
         {
             if (rigidbody2D.velocity.x < this.MaxSpeed) 
             {
                 rigidbody2D.AddForce (new Vector2(this.Acceleration, 0.0f));
             } 
             else 
             {
                 rigidbody2D.velocity = new Vector2(this.MaxSpeed, rigidbody2D.velocity.y);
             }        
         }
         
         bool onTheGround = isOnGround();
         
         float vertical = Input.GetAxis("Vertical");
         
         if(onTheGround)
         {
             canDoubleJump = true;
         }
 
         if(vertical > 0.1f)
         {
             if (!jumpKeyDown) 
             {
                 jumpKeyDown = true;
                 
                 if(onTheGround || (canDoubleJump && EnableDoubleJump) || wallHitDoubleJumpOverride)
                 {
                     bool wallHit = false;
                     int wallHitDirection = 0;
                     
                     bool leftWallHit = isOnWallLeft();
                     bool rightWallHit = isOnWallRight();
                     
                     if(horizontal !=0)
                     {
                         if(leftWallHit)
                         {
                             wallHit = true;
                             wallHitDirection = 1;
                         }
                         else if(rightWallHit)
                         {
                             wallHit = true;
                             wallHitDirection = -1;
                         }
                     }
                     
                 
                     if(!wallHit)
                     {
                         if(onTheGround || (canDoubleJump && EnableDoubleJump))
                         {
                             rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x, this.JumpSpeed);
                         
                             JumpDuration = 0.0f;
                             canVariableJump = true;
                         
                         }
                     }
                     else 
                     {
                         rigidbody2D.velocity = new Vector2(this.JumpSpeed * wallHitDirection, this.JumpSpeed);
                     
                         jmpDuration = 0.0f;
                         canVariableJump = true;
                     }
                     if(!onTheGround && !wallHit)
                     {
                         canDoubleJump = false;
                     }
                 }
             }
             else if(canVariableJump)
             {
                 jmpDuration += Time.deltaTime;
 
                 if(jmpDuration < this.JumpDuration /1000)
                 {
                     rigidbody2D.velocity = new Vector2 (rigidbody2D.velocity.x, this.JumpSpeed);
                 }
             }
         }
         else
         {
             jumpKeyDown = false;
             canVariableJump = false;
         }
     }
 
     //Methods
 
     private bool isOnGround()
     {
         float lengthToSearch = 0.1f;
         float colliderThreshold = 0.1f;
 
 
         Vector2 linestart = new Vector2 (this.transform.position.x, this.transform.position.y - this.renderer.bounds.extents.y - colliderThreshold);
 
         Vector2 vectorToSearch = new Vector2 (this.transform.position.x, linestart.y - lengthToSearch);
 
         RaycastHit2D hit = Physics2D.Linecast (linestart, vectorToSearch);
         return hit;
 
 
     }
     private bool isOnWallLeft()
     {
         bool retVal = false;
 
         float lengthToSearch = 0.1f;
         float colliderThreshold = 0.1f;
 
         Vector2 linestart = new Vector2 (this.transform.position.x - this.renderer.bounds.extents.x - colliderThreshold, this.transform.position.y);
         Vector2 vectorToSearch = new Vector2 (linestart.x - lengthToSearch, this.transform.position.y);
 
         RaycastHit2D hitLeft = Physics2D.Linecast (linestart, vectorToSearch);
 
         retVal = hitLeft;
 
         if (retVal) 
         {
             if(hitLeft.collider.GetComponent<NoSlideJump>())
             {
                 retVal = false;
             }
         }
 
         return retVal;
     }
 
     private bool isOnWallRight()
     {
         bool retVal = false;
         
         float lengthToSearch = 0.1f;
         float colliderThreshold = 0.1f;
         
         Vector2 linestart = new Vector2 (this.transform.position.x + this.renderer.bounds.extents.x + colliderThreshold, this.transform.position.y);
         Vector2 vectorToSearch = new Vector2 (linestart.x + lengthToSearch, this.transform.position.y);
         
         RaycastHit2D hitRight = Physics2D.Linecast (linestart, vectorToSearch);
         
         retVal = hitRight;
         
         if (retVal) 
         {
             if(hitRight.collider.GetComponent<NoSlideJump>())
             {
                 retVal = false;
             }
         }
         
         return retVal;
     }
 }
 
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

Making a bubble level (not a game but work tool) 1 Answer

Sprite flip movement script not working! HELP! 0 Answers

How to make sprites in SideScroller move in smooth curves 0 Answers

Sprite is not shown moving 1 Answer

Recoloring animated sprites [Solved] 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