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 WhyldH · Jun 03, 2016 at 04:13 PM · 2d game2d-physicstroubleshootingbackwards

Backsliding like a baws

Hella people !

Im't actually trying to tweak a bit the walking script of the tutorial for 2d games. Here's the thing : using UnityEngine; using System.Collections;

 public class CharacterControllerScript : MonoBehaviour
 {
     public float maxSpeed = 10f;
     bool facingRight = true;
 
     Animator anim;
     
     // Use this for initialization
     void Start ()
     {
         anim = GetComponent<Animator> ();
     }
     
     // Update is called once per frame
     void FixedUpdate () 
     {
         float move = Input.GetAxis ("Horizontal");
         anim.SetFloat ("Speed", Mathf.Abs (move));
         rigidbody2D.velocity = new Vector2 (Mathf.Sign(move) * maxSpeed, rigidbody2D.velocity.y);
         if (move > 0 && !facingRight)
             Flip ();
         else if (move < 0 && facingRight)
             Flip ();
     }
 
     void Flip ()
     {
         facingRight = !facingRight;
         Vector3 theScale = transform.localScale;
         theScale.x *= -1;
         transform.localScale = theScale;
     }
 }

As you can see, I've replaced the common "move maxSpeed" by "Mathf.Sign(move) maxSpeed". The purpose is to prevent speed variations with a joystick, so the gameplay's still the same with a keyboard and a gamepad. Also I've added kind of a "startup" and "recovery" frames, using the "curve" fonction and modulating the maxSpeed during "start walking" and "return to idle" animations.

Now here's the problem :

When the char is going left and I instantly stop to press the left key, the char is sliding backward. I've tried to test this with a keyboard and the reaction is the same. I've tried to suppress the "recovery" frames and it worsen the problem ; turning the char into an unstoppable backslide masta'.

Edit: the problem comes from the fact that "mathf.Sign()" can never return the 0 value, since it is returning 1 as for 0 or positive numbers, so if I keep suing this function I need to find a way to return 0 when the player doesn't press anything.

So if someone has a solution to keep the "no variation in speed" gameplay style, without using a clunky "if", and keeping the "startup" and "recovery" frames : I'll thank him greatly. ^^

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by WhyldH · Jun 03, 2016 at 04:58 PM

resolve the problem with this : using UnityEngine; using System.Collections;

 public class CharacterControllerScript : MonoBehaviour
 {
     public float maxSpeed = 10f;
     bool facingRight = true;
 
     Animator anim;
     
     // Use this for initialization
     void Start ()
     {
         anim = GetComponent<Animator> ();
     }
     
     // Update is called once per frame
     void FixedUpdate () 
     {
         float move = Input.GetAxis ("Horizontal");
         float side = 1;
         anim.SetFloat ("Speed", Mathf.Abs (move));
 
         if(facingRight == true)
             side = 1;
         else
             side = -1;
 
         if (move != 0)
             rigidbody2D.velocity = new Vector2 (Mathf.Sign(move) * maxSpeed, rigidbody2D.velocity.y);
         else
             rigidbody2D.velocity = new Vector2 (side * maxSpeed, rigidbody2D.velocity.y);
 
         if (move > 0 && !facingRight)
             Flip ();
         else if (move < 0 && facingRight)
             Flip ();
     }
 
     void Flip ()
     {
         facingRight = !facingRight;
         Vector3 theScale = transform.localScale;
         theScale.x *= -1;
         transform.localScale = theScale;
     }
 }

Is there anything better to do ? I mean, adding a variable isn't really optimized isn't it ?

Comment
Add comment · Share
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

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

60 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

Related Questions

I want to make 2D games with unity but the Problem is I really don't Know about it,, actually i m new to this field and need Some GUIDANCE. 1 Answer

when i Drag the Block the 2d Collider Doesn't Follow it. 0 Answers

Character Feels Slow and Non-Responsive 0 Answers

Unity 2d C# - Orbit object around another objects center using mouse position 0 Answers

WheelJoint2D rotates parent object 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