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 question was closed Dec 09, 2014 at 01:15 PM by MolochSim for the following reason:

Answered in comments

avatar image
0
Question by MolochSim · Oct 27, 2014 at 03:51 PM · addforceplatformer

How to create a gravitational pull effect on object?

I am trying to make a black hole kind of obstacle in a 2D platformer I am working on. I will have to fine tune the forces and such.

But there is a problem. The effect only seems to apply in the Y axis.

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent(typeof(CircleCollider2D))]
 public class BlackHoleScript : MonoBehaviour {
 
     private float pullForce = 0.5f;
     private float radius = 3f;
 
     void Awake()
     {
         CircleCollider2D circleCollider2D= GetComponent<CircleCollider2D>();
         radius = circleCollider2D.radius;
     }
 
     void OnTriggerStay2D(Collider2D other)
     {
         if (other.gameObject.tag == "Player")
         {
             float distance = Vector2.Distance(this.transform.position, other.gameObject.transform.position);
             float force = pullForce * (radius / distance);
             float x = this.transform.position.x - other.gameObject.transform.position.x;
             float y = this.transform.position.y - other.gameObject.transform.position.y;
             Debug.Log(x);
             Debug.Log(y);
             other.gameObject.rigidbody2D.AddForce(new Vector2(x*force, y*force), ForceMode2D.Impulse);
         }
     }
 }

The x position of the object seems to be completely unaffected. What is it I am doing wrong?

Comment
Add comment · Show 7
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 MrSoad · Oct 27, 2014 at 03:53 PM 0
Share

What value is x showing in your Debug window?

avatar image Wisearn · Oct 27, 2014 at 04:01 PM 0
Share

http://wiki.unity3d.com/index.php/Gravity

You can put this script on your object and it will pull rigidbodies towards it like planetary gravity (or black hole).

Sorry for not helping with your current code, but I figured this might help so don't worry if you want to fix your own code ins$$anonymous$$d of using this.

avatar image MrSoad · Oct 27, 2014 at 04:04 PM 0
Share

Your code looks right, that's why I wondered what value you are getting for x in the Debug, you have written this in almost exactly the same way as I did for my whirlpools when I did those...

avatar image smoggach · Oct 27, 2014 at 04:07 PM 1
Share

$$anonymous$$ake sure the rigidbody does not have the axis frozen.

avatar image Baste · Oct 27, 2014 at 04:14 PM 1
Share

How do you move your player on the x-axis? If you're setting the player's rigidbody's velocity's x-value in the movement script, that will effectively null out this script's effect on the velocity.

So if you have something like this in the player script:

 rigidbody.velocity.x = Input.GetAxis("Horizontal");

That's the source of your problem.

Show more comments

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

32 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

Related Questions

Problem with Wall Jump - 2D 0 Answers

knockback 3d c# 1 Answer

how can i put a aerial dashing script on my game? 0 Answers

issue with the rigid body x axis ,addforce doesn't work as it should for x axis 1 Answer

Jump higher when held button 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