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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Ekta-Mehta-D · Oct 08, 2014 at 06:08 AM · rigidbody2dforce

Rigidbody2D Force Problem

Hello everyone..

I am making sample game like "no brakes". Here is Video Link : http://www.youtube.com/watch?v=Lb2sQJfqZnE . I have done coding for moving player.

Code :

      void Update () {
         if(ON)
         {
             rigidbody2D.drag = 1.2f;
             rigidbody2D.angularDrag = 0.05f;
             speedTxt.text = velocity.ToString ();
             SpeedControl();
             Movement();
         }
     }
 
     void Movement()
     {
         rigidbody2D.AddForce (transform.up * speed );
         velocity = Mathf.Round (rigidbody2D.velocity.magnitude * 10.6f);
 
         if(Input.GetMouseButton(0))
         {
             Vector3 inputPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
             inputPos.z = 0;
             RaycastHit2D hit;
             hit = Physics2D.Raycast( inputPos,new Vector2( 0,0 ) );
 
             if(hit.collider.name == "Left")
             {
                 Debug.Log("left.. ");    
                 yRotation += 3.0f ;
 
                 Quaternion target = Quaternion.Euler(0, 0, yRotation);
                 transform.rotation = Quaternion.Slerp(transform.rotation, target, Time.deltaTime * 10.0f);
 
                 rigidbody2D.AddForce(-transform.right * speed * 1.1f );
                 //rigidbody2D.AddForce (transform.up * 1.2f );
                 rigidbody2D.drag += 0.1f;
                 //rigidbody2D.angularDrag += 0.5f;
             }
             
             if(hit.collider.name == "Right")
             {
                 Debug.Log("Right.. ");    
                 yRotation -= 3.0f ;
 
                 Quaternion target = Quaternion.Euler(0, 0, yRotation);
                 transform.rotation = Quaternion.Slerp(transform.rotation, target, Time.deltaTime * 10.0f);
 
                 rigidbody2D.AddForce(transform.right * speed * 1.1f );
                 //rigidbody2D.AddForce (transform.up * 1.2f );
                 rigidbody2D.drag += 0.1f;
                 //rigidbody2D.angularDrag += 0.5f;
             }
         }
     }

All this i have did in 2d scene.

In this code issue is while moving left and right.. its not going smoothly like it should go forward also and should turn also. I want exact behavior like in this video.

I am trying from last two days but i am not able to solve this.

Please anyone help me.

Thanks,..

[1]: http://www.youtube.com/watch?v=Lb2sQJfqZnE

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 zharik86 · Oct 08, 2014 at 06:57 AM

First, your angle, yRotation is equal to 3 degrees. It is difficult to see such deviation on a scene. Reject your object of degrees on 20. Also set the speed of change of a deviation, for example, 5. The second, in your script I don't see resetting to an original status of rotation in case of release of the button. I will a little rewrite your script as I represent it:

  public float speedRot = 5.0f; //add one more variable for speed of rotation. For change value, change it in Inspector.

  void Start() {
   //Maybe, initialization your rigidbody here
   
  }

  void Update () {
   if(ON) {
    rigidbody2D.drag = 1.2f;
    rigidbody2D.angularDrag = 0.05f;
    speedTxt.text = velocity.ToString ();
    SpeedControl();
    Movement();
   }
  }

  void Movement() {
   rigidbody2D.AddForce (transform.up * speed );
   velocity = Mathf.Round (rigidbody2D.velocity.magnitude * 10.6f);

   if(Input.GetMouseButton(0)) {
    Vector3 inputPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    inputPos.z = 0;
    RaycastHit2D hit;
    hit = Physics2D.Raycast( inputPos,new Vector2( 0,0 ) );

    if(hit.collider.name == "Left") {
     Debug.Log("left.. ");   
     yRotation += 20.0f; //change angle to 20 degree

     Quaternion target = Quaternion.Euler(0, 0, yRotation);
     //Apply our variable speedRot
     transform.rotation = Quaternion.Slerp(transform.rotation, target, Time.deltaTime * speedRot);

     rigidbody2D.AddForce(-transform.right * speed * 1.1f );
     //rigidbody2D.AddForce (transform.up * 1.2f );
     rigidbody2D.drag += 0.1f; //Why change, you reinitialization your rigidbody in Update every frame. Isn't necessary.
     //rigidbody2D.angularDrag += 0.5f;
    }

    if(hit.collider.name == "Right") {
     Debug.Log("Right.. ");  
     yRotation -= 20.0f;

     Quaternion target = Quaternion.Euler(0, 0, yRotation);
     transform.rotation = Quaternion.Slerp(transform.rotation, target, Time.deltaTime * speedRot);

     rigidbody2D.AddForce(transform.right * speed * 1.1f );
     //rigidbody2D.AddForce (transform.up * 1.2f );
     rigidbody2D.drag += 0.1f;
     //rigidbody2D.angularDrag += 0.5f;
    }
   }
  }

I hope that it will help you.

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 Ekta-Mehta-D · Oct 08, 2014 at 07:13 AM 0
Share

No in this game its not resetting the rotation. It will move towards that direction. U can play that game. But while moving its stopes moving forward.

avatar image bhartu · Oct 08, 2014 at 11:15 AM 0
Share

why you are not writing in FixedUpdate() any reason?

avatar image zharik86 · Oct 08, 2014 at 06:38 PM 0
Share

Ok, remove line with "else".

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

28 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

Related Questions

A node in a childnode? 1 Answer

Make Bumper Effect 0 Answers

Adding Foward Force to A Rigidbody2D 0 Answers

Rigidbody2D won't stop moving. 2 Answers

Rigidbody is Pushing Another Rigidbody : Unity 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