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 /
avatar image
0
Question by Midori48 · Feb 25, 2018 at 04:05 PM · 2d game2d-physics2d animationrunningspeed issues

How do i increase Speed while running 2d

Yoh guys, i have two issues, the first is that when i run even if I release the key (R) the character keeps running, the second is that I can not grow the speed: so both when walking and when running, the speed remains the same. Someone can help me?

public class Movement : MonoBehaviour {

 public float speedmultiplier = 100.0f;

 public float runSpeed = 50f;
 public Rigidbody2D rb2d;
 private float currentMaxSpeed;
 bool facingRight = true;
 public float speed= 10f;
 public Animator animator;

// Use this for initialization

 void Start () 

 {
 animator = GetComponent<Animator> ();
 rb2d = GetComponent<Rigidbody2D> ();

 }




 void Update ()
 {
         


     if (Input.GetKey (KeyCode.D)) 
     {
         transform.Translate (new Vector2 (1, rb2d.velocity.y) * Time.deltaTime * speedmultiplier);
     } 

     else if (Input.GetKey (KeyCode.A)) 
     {
         transform.Translate (new Vector2 (-1, rb2d.velocity.y) * Time.deltaTime * speedmultiplier);
     }

     if (Input.GetAxis ("Horizontal") > 0 && !facingRight) 

     {
         rb2d.AddForce (transform.right * speedmultiplier);

         Flip ();

         animator.Play ("Walk");

     } 

     if (Input.GetAxis ("Horizontal") < 0 && facingRight) 


     {
         rb2d.AddForce (transform.right * speedmultiplier);

         Flip ();

         animator.Play ("Walk");
     } 

         else if (Input.GetAxisRaw ("Horizontal") == 0) 
         
         {
             rb2d.velocity = new Vector2 (0, rb2d.velocity.y);
             animator.Play ("Idle");
         }


     if (Input.GetKey (KeyCode.R)) 
     
     {
         animator.speed = 30f;

         animator.Play ("Run");

         bool Run = true;

         rb2d.AddForce (transform.right * speedmultiplier);


     }
             
             
         float speed = currentMaxSpeed * Input.GetAxis ("Horizontal");
         animator.SetFloat ("speed", Mathf.Abs (speed));
         transform.Translate (Vector2.right * speed * Time.deltaTime);
         
         
 }


     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 AmirSavand · Feb 25, 2018 at 04:36 PM

You need to have a variable for changing speed when running, keep the variable at 1f and multiply it by the final force you give the rigidbody for speed, then if you increase that value like make it 2f, the speed will be 2x faster.

Comment
Add comment · Show 4 · 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 AncillaryJustice · Feb 25, 2018 at 05:07 PM 0
Share

Can you please give an example trough the script? Never done it and i am really a beginner..

avatar image AmirSavand AncillaryJustice · Feb 25, 2018 at 05:17 PM 0
Share

Well I can not code for you, but you need to increase the velocity, that's it, that's all you need o do.

avatar image AmirSavand AncillaryJustice · Feb 25, 2018 at 05:19 PM 0
Share

Your code is pretty messy too.


Don't use key codes, use input names from player inputs. Don't use condition on facing left or right.


Try looking up some tutorials or wait for someone to write code for you.

avatar image LexLthr · Jun 15, 2018 at 01:08 PM 0
Share

I'm making a 3d infinite runner and my characters speed increments everytime he LevelsUp. but the problem is that he doesn't gradually increment, ins$$anonymous$$d, he goes from normal to very fast and its hard to control him. here's my code:

 public class PlayerController : $$anonymous$$onoBehaviour
 {
     private CharacterController controller;
     private float speed = 2.0f;
     private Vector3 moveVector;
     private float vertivalVelocity = 0.0f;
     private float gravity = 12.0f;
     private float animationDuration = 2.0f;
     private bool isDead = false;
 
     // Use this for initialization
     void Start ()
     {
         controller = GetComponent<CharacterController>();
     }
     
     // Update is called once per frame
     void Update ()
     {
        
         if (isDead)
             return;
         if(Time.time < animationDuration)
         {
             controller.$$anonymous$$ove(Vector3.forward * speed * Time.deltaTime);
             return;
  }
 
         moveVector = Vector3.zero;
 
         if(controller.isGrounded)
         {
             vertivalVelocity = -0.5f;
         }
         else
         {
             vertivalVelocity -= gravity * Time.deltaTime;
         }
 
         //x - Left and Right
         moveVector.x = Input.GetAxisRaw("Horizontal") * speed;
         //y - Up and Down
 
         //z - Foward and Backward
         moveVector.z = speed;
 
         controller.$$anonymous$$ove((moveVector * speed) * Time.deltaTime);
     }
     public void SetSpeed(float modifer)
     {
         speed = 2.0f * modifer;
     }
 
     //it is being called everytime our capsule hits something
  private void OnControllerColliderHit(ControllerColliderHit hit)
     {
         if (hit.point.z > transform.position.z + 0.1f && hit.gameObject.tag == "Enemy")
             Death();
     }
 
     private void Death()
     {
         isDead = true;
         GetComponent<Score>().OnDeath (); 
     }

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

88 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 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

Stuttering in simple 2D game using interpolation? 1 Answer

2D Z-Levels HELP Needed! Can I script multiple objects to run on z levels,,2d Multiple Z levels in one scene 0 Answers

Having some stuck issues on the 2D infinite runner 0 Answers

What is the best/easiest way to align a 2D rigidbody to specific sections of a sprite? 1 Answer

How to flip 2d character walk movement? 3 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