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 RingK · May 16, 2016 at 02:03 PM · unity 52d gamerigidbody2d

Drag to add power, release to throw at opposite angle (RigidBody2D), strange beahviour.

I'm a total beginner and I'm making a simple 2D game, you have ball which you throw to collect pickups. I managed, following tutorials and searching on the web, to script the controls as I wanted them, when the mouse is clicked the force to apply is calculated by the movement on axis X and Y and on mouse button release the force is applied and the gravity turned on:

     if (Input.GetMouseButton(0)) mouseDown = true;
     if (!Input.GetMouseButton(0))  mouseDown = false;         

     if (Input.GetAxis("Mouse X") != 0 && mouseDown)
     {            
         xForce -= Input.GetAxis("Mouse X") * 50;
     }

     if (Input.GetAxis("Mouse Y") != 0 && mouseDown)
     {
         yForce -= Input.GetAxis("Mouse Y") * 50;
     }      

     if (Input.GetMouseButtonUp(0))
     {
         GetComponent<Rigidbody2D>().gravityScale = 1;
         GetComponent<Rigidbody2D>().AddForce(new Vector2(xForce, yForce));
         yForce = 0;
         xForce = 0;
     }

But while working perfectly while testing it on unity using the mouse, it behave strangely on my 2 android phones. Sometimes the ball seems to get the force only for the X axis, and even if dragging only vertically the ball has an X force applied... What could be the problem?

EDIT: I added Debug.Log(xForce) and Debug.Log(yForce) and looking at the log with adb logcat I noticed that, also when simply tapping on the screen, without dragging on neither axis, the value of those variables increase, I have really no idea on how to solve this...

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

3 Replies

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

Answer by Firedan1176 · May 16, 2016 at 04:00 PM

Those values for Input.GetAxis are delta values, so it is the difference in movement of the mouse since the last update frame. It may not always be accurate. What I would recommend you doing, however, is get the start position of the mouse when they first click, and the end position when they release. I would get the difference between that and simply use that as the x and y force values. So something like this:

 Vector2 start, end, force;
 
 void Update() {
     if(Input.GetMouseButtonDown(0)) start = Input.mousePosition;
     else if(Input.GetMouseButtonUp(0)) {
         end = Input.mousePosition;
         force = end - start;
     }
 }

If you want to try that out, let me know how that works. It is just a start, so I do not know how it will work in your project.

Comment
Add comment · Show 3 · 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 RingK · May 16, 2016 at 06:13 PM 0
Share

Thanks a lot @Firedan1176 you pointed me to the right direction (after days of web searching), and after some experimenting I ended up with what I wanted, here's my code, I get the mouse position when button is clicked (or screen touched):

 if (Input.Get$$anonymous$$ouseButtonDown(0)) 
     {            
         startPosX = Input.mousePosition.x;
         startPosY = Input.mousePosition.y;
     }

then when the mouse button is released I store the position of it and store in 2 variables xForce and yForce the difference between start and end position, and make a Vector2 with those variables to add a force to the rigidbody, then reset everything to be ready for the second shot:

 if (Input.Get$$anonymous$$ouseButtonUp(0))
     {
         endPosX = Input.mousePosition.x; 
         endPosY = Input.mousePosition.y;

         xForce = startPosX - endPosX;
         yForce = startPosY - endPosY;
        
         GetComponent<Rigidbody2D>().gravityScale = 1;
         GetComponent<Rigidbody2D>().AddForce(new Vector2(xForce, yForce)); 
                    
         yForce = 0;
         xForce = 0;
         startPosX = 0;
         startPosY = 0;           
     }     

It works with both mouse and touch controls, I'm pretty happy with it :) Should I mark your question as "accepted" or should I post another answer with my full code?

avatar image Firedan1176 · May 16, 2016 at 06:17 PM 0
Share

@Ring$$anonymous$$, I'm glad it was helpful. In your comment, you are getting endPositionY and endPositionX as separate variables, which are probably floats in your script? However, it will save you time if you simply keep it as a Vector2 like in my example. Remember, a Vector2 is only 2 floats stored in 1 variable. That way, you save your time. Also, Input.mousePosition returns a Vector2, so you are really making extra work for yourself. Good luck, and keep on coding!

avatar image RingK · May 16, 2016 at 06:40 PM 0
Share

@Firedan1176 Thanks! I broke it down to separate values because I was getting problems "normalizing" negative values, I should revert them to a Vector2 :)

avatar image
1

Answer by RingK · Sep 01, 2017 at 08:24 AM

     private float yForce;
     private float xForce;
     private float maxForceY = 900;
     private float maxForceXp = 800;
     private float maxForceXm = -800;
     private float startPosX;
     private float startPosY;
     private float endPosX;
     private float endPosY;
     private float originX;
     private float originY;    
       
      
     
     void Update () {          
 
          originX = GetComponent<Rigidbody2D>().position.x;
          originY = GetComponent<Rigidbody2D>().position.y;
         
         if (Input.GetMouseButtonDown(0)) 
         {            
             startPosX = Input.mousePosition.x;
             startPosY = Input.mousePosition.y;                
         }
                   
         if (Input.GetMouseButtonUp(0))
         {               
             endPosX = Input.mousePosition.x; 
             endPosY = Input.mousePosition.y;
 
             xForce = (startPosX - endPosX) * 3;
             yForce = (startPosY - endPosY) * 2;
 
             if (yForce < 0) yForce = 0;
             if (yForce > maxForceY) yForce = maxForceY;
             if (xForce > 0 && xForce > maxForceXp) xForce = maxForceXp;
             if (xForce < 0 && xForce < maxForceXm) xForce = maxForceXm;
 
             GetComponent<Rigidbody2D>().gravityScale = 1;
             GetComponent<Rigidbody2D>().AddForce(new Vector2(xForce, yForce)); 
                        
             yForce = 0;
             xForce = 0;
             startPosX = 0;
             startPosY = 0;   
             endPosX = 0;
             endPosY = 0;             
         }           
     }   
    
 

This is what I ended up with, hope it helps. Surely, it can be improved ;)

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 Mignart · Sep 02, 2017 at 06:05 PM 0
Share

Super appreciated!

avatar image
0

Answer by Mignart · Aug 31, 2017 at 06:29 PM

Hey @RingK ,

Can you post the full script so that those of who are learning can get the full reference? Would be super super helpfu!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Instantiate new gameobjects on collision, keeping velocity, up to a cap 1 Answer

Best Image Formats, Sizes, and Resolutions for Unity Sprites? 2 Answers

Rigidbody2d Floating Above Ground 5 Answers

2D Character jittering after build. (Unity 5) 1 Answer

How do i stop rigidbody2D bounce 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