• 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 theBuilder10000 · Oct 16, 2019 at 02:26 AM · 2d gameplayerphysics2dforcepull

How do I make a tractor beam affect my player?

I have a player that moves around my game normally, and I have an object that has a radius around it that's meant to pull the player into its center to damage it. I'm moving the player using rb.MovePosition. I can get the player to move normally, and I can get the object to pull the player in, but I can't get both things to happen at the same time. Basically I want the player to be pulled in, but only slightly. I want them to be able to escape the pull. How do I make two opposing forces act on the same object.'

 velocity.x = joystick.Horizontal;
 velocity.y = joystick.Vertical;
 rb.MovePosition(transform.position + velocity * speed);
     
 if(attracted == true){
      tractorDirection = tractorBeam.transform.position - transform.position;
 }
 //Trying to add both the tractor direction and the velocity of the player, and to make the force towards the tractor beam less than the input velocity force.
                 

alt text

screen-shot-2019-10-15-at-102555-pm.png (279.9 kB)
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by HappiiGamer · Oct 16, 2019 at 04:24 AM

Hey, try this:

 velocity.x = joystick.Horizontal + pullVelocity.x;
 velocity.y = joystick.Vertical + pullVelocity.y;

Where your pullVelocity is

 pullVelocity = Amplitude of Pull  x Direction of the Pull

Comment
Add comment · Show 2 · 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 theBuilder10000 · Oct 16, 2019 at 05:33 AM 0
Share

Hey thank you for reply. It works perfectly.

avatar image HappiiGamer theBuilder10000 · Oct 16, 2019 at 01:45 PM 0
Share

Happy to hear that! If you want like a gravity pull that gets weaker the farther you are, you can change that Amplitude value based on the distance from your tractor beam. Good luck with your project!

avatar image
0

Answer by GrayLightGames · Oct 16, 2019 at 04:39 AM

Honestly I haven't implemented this kind of gravity, but have you tried something like:

  var tractorDirection = Vector3.zero;
  if(attracted == true){
       var tractorHeading = tractorBeam.transform.position - transform.position;
       tractorDirection = tractorHeading / tractorHeading.magnitude;
  }
 
  tractorGravity = someValueLessThanSpeed;
 
  velocity.x = joystick.Horizontal;
  velocity.y = joystick.Vertical;
  
  rb.MovePosition(transform.position + velocity * speed + tractorDirection * tractorGravity);   

If tractorGravity is less than speed, it should be possible to escape by moving directly away from it. But you can play around with the value until you have what you're looking for.

That vector subtraction gives you a direction with a magnitude(sometimes called a heading), so you have to divide out the magnitude for just the actual direction.

But if someone with more 2D physics experience can correct me, feel free. Hope that helps!

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

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

Related Questions

Game Object is moving towards the left when fire button is pressed. 2 Answers

how to have air control after a rocket jump? 0 Answers

Detect 2D joint break? 1 Answer

GameObject not detecting collision with floor 0 Answers

Calculate Force needed to push RigidBody to desiref position 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges