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 supbruh · Feb 05, 2015 at 11:21 AM · velocitydirectionchangetapon

Change the direction of velocity upon tapping a button!!??

I am trying to change the velocity of a 2d object every time I hit the space button, but every second time I hit the space bar, the object just stops moving.

here the code I am using:

using UnityEngine; using System.Collections;

public class Controller : MonoBehaviour {

 float xVel;
 int changDir=0;

 // Use this for initialization
 void Start () {
 
 }
 void Update(){
     if (Input.GetKeyDown(KeyCode.Space)){
         changDir= changDir+1;
         Debug.Log(changDir);

         if (changDir%2==1){


             xVel= -100f;
             rigidbody2D.AddForce(new Vector2(xVel,0f));
         }

         if (changDir%2==0) {


             xVel= 100f;
             rigidbody2D.AddForce(new Vector2(xVel,0f));
             

         }
                             
                                 
     }

             
 }
Comment
Add comment · Show 1
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 AlwaysSunny · Feb 04, 2015 at 09:27 PM 2
Share

Why modulus? If there's no particular reason, just do:

 bool right;
 void Update() {
     if (Input.Get$$anonymous$$eyDown($$anonymous$$eycode.Space)){ Flip(); }
 }
 void Flip() {
     right = !right;
     float xVel = (right) ? 100 : -100;
     rigidbody2D.AddForce(xVel,0);
 }




1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Baste · Feb 05, 2015 at 01:01 PM

It stops moving because you're adding an equal force in the opposite direction of the object's velocity.

The first time you tap space, you're going to add -100 force in the x direction. The next time you tap space, you're adding 100 to that -100. If you remember how maths work, then you'll recall that:

-100 + 100 = 0

Which should explain why it stops every other time

The easiest way to fix this is to set the velocity to 0 before you add the force:

 rigidbody2D.velocity = new Vector2(0,0);
 rigidbody2D.AddForce(xVel,0);

You can also make your code faaar lighter:

 float goingRight = true;

 void Update() {
     if (Input.GetKeyDown(KeyCode.Space)) {
         goingRight = !goingRight;

         float force = 100f;
         if(!goingRight)
             force = -100f;

         rigidbody2D.velocity = new Vector2(0,0);
         rigidbody2D.AddForce(force, 0);
     }
 }
Comment
Add comment · Show 1 · 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 supbruh · Feb 08, 2015 at 05:29 AM 0
Share

Thank you very much!! It solves my problem.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Cant get moving platform to go the other direction 1 Answer

Throwing knife goes straight 1 Answer

Rotation and Velocity of bullets. 1 Answer

How to get the direction of the velocity of an object (c#) 1 Answer

How do you limit velocity on a rigid body in only one direction? 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