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 Polpettone · Oct 31, 2014 at 06:47 PM · bugcharactercontroller2d-platformer

Unity 2D Character controller bug problem

Hi I have a problem with this script,the script is not bad but there are a bug where if you press A + Space and after D or the reverse,the player increases the speed, and does not change direction,please someone that help to solve this problem? The code:

 using UnityEngine;
 using System.Collections;
 
 public class CharacterController2D : MonoBehaviour {
 
     public float upSpead;
     public float jumpSpead;
     public bool jumping = false;
     public bool grounded = false;
     public string sidesHit = "none";
     public Vector2 down;
     public Vector2 sides;
     public Vector2 sideLeft;
     public float skinWidth = 0.1f;
     public float InitalAcceleration = 500;
     public float airspeed = 500;
     public bool maxSpeed = false;
 
     Vector2 start;
     RaycastHit2D hit;
     void Start(){
         //start = transform.position;
         //start.y = transform.position.y - transform.localScale.y/2f;
         }
 
     void Update () {
     //Basic ground checking code with Physics2d raycasting
         sideLeft = transform.position;
         sideLeft.x = transform.position.x - transform.localScale.x / 2 - 0.1f;
         sides = transform.position;
         sides.x = transform.position.x + transform.localScale.x / 2 + 0.1f;
         start = transform.position;
         start.y = transform.position.y - transform.localScale.y/2f - 0.1f;
         Debug.DrawRay (start, -Vector2.up);
         Debug.DrawRay (new Vector2(start.x - transform.localScale.x/2, start.y), -Vector2.up);
         Debug.DrawRay (new Vector2(start.x + transform.localScale.x/2, start.y), -Vector2.up);
         Debug.DrawRay (sides, Vector2.right);
         Debug.DrawRay (sideLeft, -Vector2.right);
 
 
         if (Physics2D.Raycast (start, -Vector2.up, skinWidth).collider != null || Physics2D.Raycast (new Vector2(start.x + transform.localScale.x/2, start.y), -Vector2.up, skinWidth).collider != null || Physics2D.Raycast (new Vector2(start.x - transform.localScale.x/2, start.y), -Vector2.up, skinWidth).collider != null) {
                         grounded = true;
                 } else {
             grounded = false;
                 }
 
     
         if (Input.GetKeyUp(KeyCode.A) && grounded == true) {
             //Detect when certain keys are released to reset velocity
             //Reset the velocity to a number close to 0 to make a sudden stop, but ease out to fell smoother
             rigidbody2D.velocity = new Vector2(-2,0);
         }
         if (Input.GetKeyUp(KeyCode.D) && grounded == true) {
             rigidbody2D.velocity = new Vector2(2,0);
         }
         //Jumping Trigger
         if (Input.GetKeyDown (KeyCode.Space)) {
             if(grounded == true){
             jumping = true;
                 //Add the Initial Accleration to make the player shoot up, and then slow down, then fall
                 rigidbody2D.AddForce(new Vector2(0,InitalAcceleration));
 
             }
                 }
         if (Input.GetKeyUp (KeyCode.Space)) {
             jumping = false;
         }
         if (Input.GetKey (KeyCode.Space)) {
             if(jumping ==true){
                 //Detect if speed limit is reached, and then fall back down
             if(rigidbody2D.velocity.y >=10){
                 jumping = false;
                 }
                 rigidbody2D.AddForce(new Vector2(0,jumpSpead * Time.deltaTime));
 
 
                 }
             }
         //Detect sides by raycastting. Possibly can be used for wall jumping
         if (Physics2D.Raycast (sides, Vector2.right, skinWidth).collider != null) {
                         sidesHit = "right";
         } else if (Physics2D.Raycast (sideLeft, -Vector2.right, skinWidth).collider != null) {
             sidesHit = "left";
                 }
             else {
             sidesHit = "none";
             
         }
 
         Move ();
 
 
 
     }
     void Move(){
         //Move in air code
         if (Input.GetAxisRaw ("Horizontal") < 0 && grounded == false && maxSpeed == true) {
             rigidbody2D.AddForce(new Vector2(airspeed * Time.deltaTime,0));
                 }
         else if (Input.GetAxisRaw ("Horizontal") > 0 && grounded == false && maxSpeed == true) {
             rigidbody2D.AddForce(new Vector2(-airspeed * Time.deltaTime,0));
         }
 
         //Move Left/Right Code
         if (rigidbody2D.velocity.x >= 9 || rigidbody2D.velocity.x <= -9) {
                         maxSpeed = true;
                 } else {
             if(maxSpeed == true){
                 maxSpeed = false;
             }
                 }
         
         if (Input.GetAxisRaw ("Horizontal") < 0 && sidesHit != "left") {
             if (maxSpeed == false && grounded == false) {
                 rigidbody2D.AddForce(new Vector2(-airspeed * Time.deltaTime,0));
                 }else if(maxSpeed == false){
             rigidbody2D.AddForce(new Vector2(-upSpead * Time.deltaTime,0));
                 }
             }
         if (Input.GetAxisRaw ("Horizontal") > 0 && sidesHit != "right") {
             if (maxSpeed == false && grounded == false && Input.GetAxisRaw ("Horizontal") < 0) {
                 rigidbody2D.AddForce(new Vector2(airspeed * Time.deltaTime,0));
             }else if(maxSpeed == false){
             rigidbody2D.AddForce(new Vector2(upSpead * Time.deltaTime,0));
             }
         }
 
     }
 }

Download Project : https://github.com/Mimerme/SuperMeatyController2D/archive/master.zip

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

Unity Backdating itself 0 Answers

Scene and hierarchy suddenly empty 0 Answers

Normal map color change problem. BUG? 1 Answer

A node in a childnode? 1 Answer

Click and drag - Object go through other objects when dragging 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