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 /
avatar image
0
Question by Thyvo · Apr 03, 2012 at 06:14 PM · c#gameobjectphysicsgravity

Suck gameobjects to a sphere

I have created a sphere and I want to give it its own gravity (I mean when GameObjects with rigged bodies come near it they get pulled towards the sphere) (some blackhole effect)

I do not know here I need to start looking for this.

I'm hoping someone can tell me where I need to look for this, or give a example c# script

Greetings Thyvo

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
2

Answer by Xeong-Hu · Mar 23, 2015 at 09:52 AM

Try this out man!

 using UnityEngine;
 using System.Collections;
 
 public class Tester : MonoBehaviour {
 
     public GameObject PullOBJ;
     public float ForceSpeed;
 
     public void OnTriggerStay (Collider coll) {
         
         if (coll.gameObject.tag == ("Pullable")){
             PullOBJ = coll.gameObject;
 
             PullOBJ.transform.position = Vector3.MoveTowards
                 (PullOBJ.transform.position,
                  transform.position,
                  ForceSpeed * Time.deltaTime);
         } 
     }
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 }
 

Depending if you're using a Sphere or a Circle, add Sphere/Circle Collider to your Vortex GameObject, Then Duplicate that Sphere, add it as Child of your Vortex GameObject, and delete the Mesh renderer within it.

Then add the script I just posted into the Vortex's Child Object.

Next set that Child Object as a Trigger and name it as PullForce or Something.

Then, the rest is simple.

Duplicate as many PullForces Objects as you want and increase the Radius as you continue.

Now as the gameobject get's closer, Force's pull will become stronger. And as it falls away from it, vice versa.

Tested it, and it worked fine.

Hope that helps dude!

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
avatar image
1

Answer by gameplay4all · Mar 23, 2015 at 10:12 AM

This is exactly what you need!

https://www.youtube.com/watch?v=gHeQ8Hr92P4

https://www.youtube.com/watch?v=TicipSVT-T8

Credits to Sebastian Lague, he makes awesome tutorials!

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
avatar image
0

Answer by schaddem · Apr 03, 2012 at 11:24 PM

I'd try it like this: make a script you attach to every gameobject you want to check. i'll call your gravity object planet. 1. check the distance between rigid body and planet 2. If distance is smaller than the range prepare to affect it with a force 3. Calculate the vector between the 2 positions so we know where we're being drawn 4. Add the force based on distance

 var distance = Vector3(transform.position, planet.transform.position);
 if (distance<range)
 {
 
 var dif = planet.transform.position-transform.position;
 //thats the vector3 that gives us the direction where we're being drawn to
 
 var maxforce = 1000;
 var forcestrength = distance/range * maxforce;   
 // (distance/range) will give a porcentage 0.0-1.0. we calculate how much porcent of the maximum force should be applied based on distance
 
 rigidbody.AddForce(direction, ForceMode.Force);
 }

note: this code is from the top of my head and more than likely to contain errors, but you get to see what relevant functions are called. You might need an other ForceMode (or AddConstantForce or something like that), but I think ForceMode.Force is just right for gravity. Also the forcestrength is interpolated in a linear fashion (x=y) and it should probably be Power of two (x=PowerOfTwo(y)). And maxforce=1000 might be way too high/low. Other thing to note is that this is physics code and should be contained in FixedUpdate().

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Calculating the Trajectory of Planetary Bodies 1 Answer

walk on walls (third person) 0 Answers

How can I make a physics base projectile move along the path of a sphere? 1 Answer

Gravity Simulator - Script execution order 0 Answers

(C#) Gravity Field generated in a collider 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