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 S_Byrnes · May 25, 2016 at 08:20 AM · c#2dgravityballorbit

Can anyone tell me what I'm doing wrong here?

Hi all, I'm trying to create a planetary body type script to have objects fall in and out of orbit based on their original velocity multiplied by the force of gravity emitted by the planet in the direction of the planet.

I've tried what I thought would have worked, but for some reason absolutely nothing is happening to my objects (the script is attached to them).

They are prefabs that are instantiated at random locations but when they collide with the gravity trigger I need them to have force added to them in the direction of the planet.

Here is what I've got so-far:

     public float gravityAmount = 100;
     float moveSpeed;
 
     Rigidbody2D rb;
 
     public GameObject ball;
 
     bool insideGravity;
 
     void Awake()
     {
         ball = GameObject.FindGameObjectWithTag("Damager");
     }
 
     void Start()
     {
         insideGravity = false;
 
         rb = GetComponent<Rigidbody2D>();
     }
 
     void Update()
     {
 
         ball = GameObject.FindGameObjectWithTag("Damager");
 
         if (insideGravity)
         {
             //This line is the trouble I'm pretty sure.
             rb.AddForce((ball.transform.position - transform.position) * gravityAmount * Time.smoothDeltaTime);
         }
 
         Vector2 movement = new Vector2(xDirection, yDirection).normalized;
 
         rb.velocity = movement * moveSpeed;
     }
 
     void OnTriggerEnter2D(Collider2D col)
     {
         if (col.gameObject.tag == "Magnet")
         {
             insideGravity = true;
         }
     }
 }

I also thought it could have been using both AddForce and velocity at the same time so I tried using this line instead:

 rb.velocity = ball.transform.position - transform.position * gravityAmount * Time.smoothDeltaTime;

But still no change. I'd really appreciate any help you can give me, thanks!

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 DiegoSLTS · May 26, 2016 at 01:54 AM

There are a few things that could be wrong, even outside that code. I know some stuff look like too basic, but check anyway:

  • Make sure the script is enabled when the prefab enters the magnet area

  • Make sure that both the object with the script and the planet have colliders enabled

  • Make sure the planet's collider is marked as trigger

  • At least one of the objects must also have a RigidBody2D (it looks like you have that covered)

  • Check the layers of the planets and the prefab and make sure their collision is checked in the collision matrix: http://docs.unity3d.com/Manual/LayerBasedCollision.html

  • Make sure that OnTriggerEnter2D is being called, and, more important, that the line "insideGravity = true;" is being executed. Just write something to the console before that line

  • Make sure xDirection and yDirection (where are those defined??) are not 0

  • Make sure moveSpeed is not 0

  • Make sure there are no errors or warnings in the console

  • Make sure the Damager and Magnet tags are correctly set in the objects

Also, doing physic stuff in the Update function is not recomended, even if you use smoothDeltaTime. You should change physic values in FixedUpdate if you want to get consistent results independant of the FPS of your game.

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 S_Byrnes · May 26, 2016 at 02:03 AM 0
Share

Thanks for that, I appreciate the feedback, I actually did write a Debug.Log earlier and tried it out to make sure, turns out yes it was being set to true correctly.

Now I've tried moving my object using AddForce even when not inside the gravity field, it gives a more realistic result (before it just locked there after changing the value of the gravityAmount).

But the issue is that it isn't moving at a constant speed anymore, it starts of at nearly 0 and continues to apply the force every frame.

The other issue is that I had it where it couldn't leave certain X and Y bounds (screen) when it was being moved with Rigidbody2D.velocity. Now it just flys past it but slowly slows down to try to get back (due to already having force applied that it's got to fight past.

Here's where I'm at now:

 void FixedUpdate()
     {
         if (insideGravity)
         {
             Debug.Log("Is About To Add Force");
             rb.AddForce((GameObject.Find("Ball").transform.position - transform.position) * 4000f * Time.deltaTime);
         }
 
         Vector2 movement = new Vector2(xDirection, yDirection).normalized;
 
         rb.AddForce(movement * moveSpeed);
 
 if (rb.position.y >= Y_$$anonymous$$ax)
         {
             yDirection = -1;
         }
 
         if (rb.position.x >= X_$$anonymous$$ax)
         {
             xDirection = -1;
         }
 
         if (rb.position.y <= Y_$$anonymous$$in)
         {
             yDirection = 1;
         }
 
         if (rb.position.x <= X_$$anonymous$$in)
         {
             xDirection = 1;
         }
 }

Thanks for the FixedUpdate advice, I've implemented it. Any ideas on what to do next?

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

164 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 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 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 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 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 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

I am trying to launch a 2d object toward the mouse. 1 Answer

Jump and run on a circle (2D) 0 Answers

How to make ball bounce off screen boundaries? 1 Answer

Flat 2D bounce against gravity? 2 Answers

2D Top Down Character being Pulled down and movement script not working? 2 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