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 /
This question was closed Sep 15, 2016 at 07:56 PM by Elunius for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Elunius · Sep 14, 2016 at 12:27 PM · rigidbody2dvelocityaddrelativeforcerelative movement

Adding speed relative to move vector (Rigidbody2D)

Hi, i'm currently working on a arcanoid clone and have a problem:

What i want: To increase the speed of my ball relative to it's movement-vector.

What i tried: A LOT. I crawled through so many answers here but got nothing to work.

Here is what i have:

 using UnityEngine;
 using System.Collections;
 
 public class Ball : MonoBehaviour
 {
     public bool hasStarted = false;
     public bool tweaked;
     public bool speedTweaked;
     public float speed;
     public Vector2 thrust = new Vector2 (0.5f, 0f);
         
     void Update ()
     {
         if (!hasStarted) {
             if (Input.GetMouseButtonDown (0)) {
                 hasStarted = true;
                 rigidbody2D.velocity = new Vector2 (3f, 10f);
             }
             if (!speedTweaked){
                 rigidbody2D.AddRelativeForce(thrust); // Does nothing lol
                 Debug.LogError("uhm..");
             } else if (speedTweaked) {
                 rigidbody2D.AddRelativeForce(thrust); // Does also nothing omg
                 Debug.LogError("ok...");
             }
         }
     }
     
     void OnCollisionEnter2D (Collision2D col)
     { 
         //add some randomness - this is also the cause of speed loss/ gain
         Vector2 tweak = new Vector2 (Random.Range (-0.2f, 0.2f), Random.Range (-0.2f, 0.2f));
     
         if (hasStarted) {
             speed = rigidbody2D.velocity.magnitude;
             Debug.Log (speed);
             
             if (!tweaked) {
                 rigidbody2D.velocity += tweak;
                 Debug.Log ("Tweaked");
                 tweaked = true;
             } else {
                 rigidbody2D.velocity -= tweak;
                 Debug.Log ("Tweaked");
                 tweaked = false;
             }
             
             if (speed <= 11) {
                 Debug.LogError ("Speed is under 10");
                 speedTweaked = false;    
             } else if (speed >= 12) {
                 Debug.LogError ("Speed is over 12");
                 speedTweaked = true;
             }
         }
     }
 }

I would appreciate some ideas.

Greetings

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

  • Sort: 
avatar image
1
Best Answer

Answer by Elunius · Sep 16, 2016 at 12:41 PM

i figured out myself:

 void AdjustSpeed ()
     {
         speed = rigidbody2D.velocity.magnitude;
         
         if (speed < 11) {
             rigidbody2D.AddRelativeForce (rigidbody2D.velocity.normalized * 10);
         } else if (speed > 11) {
             rigidbody2D.AddRelativeForce (rigidbody2D.velocity.normalized * -10);
         }
     }

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 Sinnii · Sep 16, 2016 at 12:49 PM 0
Share

Don't you want AddForce ins$$anonymous$$d of AddRelativeForce? $$anonymous$$eep in $$anonymous$$d the velocity vector is in global space, but AddRelativeForce uses local space.

avatar image Elunius Sinnii · Sep 16, 2016 at 03:54 PM 0
Share

Could you please descripe the difference?

avatar image Sinnii Elunius · Sep 16, 2016 at 07:48 PM 0
Share

Let's say your input vector(rigidbody2D.velocity.normalized * 10) equals (10f, 0f). Let's also say your object's rotation around the Z axis is 180 degrees. If you use AddRelativeForce with that input vector, you'll be making the object go left. If you use AddForce, you'll be making the object go right.

With 0 rotation on the object, they are equal. With 90 or 270 degrees rotation, it'll be going up or down respectively, with that specific input vector.

As a side note, it would be wise to use rigidbody2D.velocity.sqr$$anonymous$$agnitude ins$$anonymous$$d, since it's much cheaper to calculate due to not needing a squareroot calculation internally. If you do so, you'll have to replace "11" with "11 * 11" ins$$anonymous$$d, to get the square. The result of any greaterthan/lessthan comparisons will be the same.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Move an enemy on top of a moving platform 1 Answer

Set Object Rotation based on Collision (2D) 0 Answers

how to make an 2d object move the direction its facing using rigidbody2d.velcoity 1 Answer

Unpausing While Keeping Values 0 Answers

Move car at same speed 0 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