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 Splashh · Mar 05, 2014 at 04:50 PM · 2dacceleration

acceleration in a 2D game (easy question, probably)

I was watching a 2D character controller tutorial

http://unity3d.com/learn/tutorials/modules/beginner/2d/2d-controllers

but I got stuck somewhere.

The video uses a script in which the movement speed accelerates. So instead of just either WALKING or NOT WALKING, the script stands still, then goes through an acceleration process in which he will first walk slowly and then quicker until max speed. I'm trying to make a 2D street fighting game as first project, so this doesnt really work for me.

What do I need to edit in order to achieve this?

Here is the script:

. . . .

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 (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;
 }
 
 

}

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 Razouille · Mar 05, 2014 at 06:24 PM

 float move = Input.GetAxis ("Horizontal");

This line will return a value between -1 and 1, so if you only want to have 0 or maxSpeed you can just force 1 if value > 0 and <= 1, or -1 if value < 0 and >= -1 The velocity is set correctly after so you don't have to do something else.

Comment
Add comment · Show 2 · 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
avatar image Splashh · Mar 05, 2014 at 06:50 PM 0
Share

Thanks for your reply!

I have no idea how to do what you've told me though.. Could you make a small code for me?

avatar image Razouille · Mar 05, 2014 at 07:38 PM 0
Share

With the code i gave you earlier, add this :

 if (move > 0.1f) {
       move = 1f;
     }
     else if (move < -0.1f) {
       move = -1f;
     }
     else {
       move = 0;
     }

So "move" will be only 0, -1 or 1

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

21 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

Related Questions

Assets/Scripts/PlayerController.cs(32,49): error CS0126: An object of a type convertible to `float' is required for the return statement 1 Answer

Character Motor: standard vs. custom ? 0 Answers

2D Animation does not start 1 Answer

2D acceleration code does not work properly 0 Answers

2D acceleration code doesnt work properly and needs slight adjustment for my needs 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