Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 oscar_wong67 · Jul 23, 2015 at 12:36 PM · 2dphysicsrigidbodyvelocityaddforce

AddForce vs Velocity issues with Rigidbody2D

I'm trying to make a basic 2D game, where the character controls a 2D hand sprite and needs to flip a pizza. I'm able to use Rigidbody.AddForce to the pizza (when the player inputs, for now I've set that to the W key), however to make it work I have to have Gravity Scale set to 0, which means the pizza can't fall back down. This prevents you from being able to further play the game.

If I set the Y-axis velocity when the player inputs W, you can force the pizza to go up even though it's no longer in contact with the hand. I can't seem to figure out a solution, as I haven't worked with Unity's physics much.

The code is in UnityScript, by the way.

 #pragma strict
 
 var collided = false;
 var thrust: float;
 var pizza = GameObject.Find("pizza-small").GetComponent.<Rigidbody2D>();
 
 //hand + pizza collision detection
 function OnCollisionEnter2D(coll: Collision2D) {
     if (coll.gameObject.tag == "Pizza") {
         collided = true;
         Debug.Log("Hand+Pizza");
     }
 }
 
 
 function Update() {
 
     //Input
     if (Input.GetKey(KeyCode.W) && collided === true) {
         Debug.Log("Up");
         transform.position.y+= 0.2;
         //Adding force to the pizza
         pizza.velocity = Vector3(0,5,0); //or use pizza.AddForce(transform.up * thrust); - AddForce must have gravity scale set to 0, negating gravity. issue with velocity is that you add velocity, you add force even though hand isn't in contact
     }
     else if (Input.GetKey(KeyCode.A) || Input.GetKey("left")) {
         Debug.Log("Left");
         transform.position.x-= 0.1;        
     }
     else if (Input.GetKey(KeyCode.D) || Input.GetKey("right")) {
         Debug.Log("Right");
         transform.position.x+= 0.1;        
     }
     else if (Input.GetKey(KeyCode.S) || Input.GetKey("down")) {
         Debug.Log("Down");
         transform.position.y-= 0.1;        
     }
     else if (transform.position.y > -3.5) {
         transform.position.y-= 0.25;
     }
 
 
 
 }
 

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Ali_Jaffer · Jul 23, 2015 at 01:17 PM

Try this. hope this will solve your problem

  if (Input.GetKey(KeyCode.W) && collided === true) {
          Debug.Log("Up");
          transform.position.y+= 0.2;
          //Adding force to the pizza
          pizza.velocity = Vector3(0,5,0); 
          collided = false;
      }
 
 if(collided == false)
 {
     debug.Log("down");
     pizza.velocity = Vector3(0,0,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 Ali_Jaffer · Jul 24, 2015 at 12:52 PM 0
Share

my answer was almost same check the code you was adding velocity when collided = true; but you was not changing the value of collided= false therefore it remain always true

avatar image
0

Answer by oscar_wong67 · Jul 24, 2015 at 10:40 AM

I solved it, I just had to make the collided variable stay false unless the proper conditions were met. I got velocity to work, so I'll just use that.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Velocity Movement & Physics Interactions by Rigidbody2D 0 Answers

Inconsistent Rigidbody2D velocity. 0 Answers

AddForce with 2D Character 2 Answers

Countering AddForce by Modifying velocity 3 Answers

Why does writing to rigidbody.velocity after AddForce stop my rigidbody moving? 4 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