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
1
Question by DiamondMC102 · Aug 11, 2017 at 08:39 AM · 2d gameaddforce2d-physicspolygon collider 2d

Add Force if GameObject is detected in a Polygon Collider 2D

Let's say that I have a Player, and my player can walk, run, and jump. Also, my player has a Polygon Collider 2D and if 2D GameObjects( with a Rigidbody2D and Box Collider 2D) are detected in the Polygon Collider 2D the Player will Add Force to movable 2D GameObjects that are in the Polygon Collider 2D to be pulled towards the Player.

(One)- How can I detect if any 2D GameObjects are in the Polygon Collider? & (Two)- How can I Add Force to movable 2D GameObjects that are in the Polygon Collider 2D to be pulled towards the Player? ------ Please code and explain your answer, for I am new to unity and won't understand if you just give me directions, I don't just use your code and forget about it, I write it down in my note book line to line and review and study it on my free time

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 sadowlight123 · Aug 11, 2017 at 10:21 AM

Hello

To detect triggers follow these steps:

Prepare:

1-assign to both objects colliders (of any shape)

2-one of the colliders must have trigger option checked. (you'll find it in the inspector) for now assign this to the movable object

3-at least one of the objects has to have rigid body checked on. (your player probably needs the rigidbody and already has it)

4- assign the main object (player in your case) a tag "Player for example"

5-Create a new c# script and put it on the movable object

Code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class YourScriptName : MonoBehaviour {
 
     // Use this for initialization
     void Start () {
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 
 
     void OnTriggerEnter2D(Collider2D other)
     {
         if (other.tag == "Player")
             //Do something
     }
 
 
 
 
 }

That is all for the first question.

this is the main thing to focus on

   void OnTriggerEnter2D(Collider2D other)
         {
             if (other.tag == "Player")
                 //Do something
         }

it know that something entered in the trigger , then it checks what this thing is . Is it an object with the tag player ? if yes ->> do code , if no ignore everything.

Now for your second question which is adding force . it is simple , it should go in that //do something

   void OnTriggerEnter2D(Collider2D other)
         {
             if (other.tag == "Player")
                 //Add Force
         }

To add force you need a rigidbody so go to the movable object and add a rigidbody (if not already done)

second step you need to create a rigidbody variable and assign it like the following :

 public class YourScriptName : MonoBehaviour {
 private Rigidbody2D Rigid;
 void Start () {
 
         Rigid = GetComponent<Rigidbody2D>();
     }


Next step is to use the following line of code which takes care of adding a force:

  Rigid.AddForce(new Vector2(Value, Value));
 
 you can adjust the Value by any number that suits you for example :
 
  Rigid.AddForce(new Vector2(20, 25)); // moves in combination of x and y direction
  Rigid.AddForce(new Vector2(0, 25)); // moves in y direction
  Rigid.AddForce(new Vector2(20, 0)); // moves x  direction

To sum up your code will look something like this:

   using System.Collections;
     using System.Collections.Generic;
     using UnityEngine;
     
     public class YourScriptName : MonoBehaviour {
        private Rigidbody2D Rigid;
        public float Value;
       void Start () {
     
             Rigid = GetComponent<Rigidbody2D>();
         }
         void Update()
 {
 }
 
    void OnTriggerEnter2D(Collider2D other)
             {
                 if (other.tag == "Player")
                     Rigid.AddForce(new Vector2(Value, Value));
             }



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 DiamondMC102 · Aug 12, 2017 at 10:44 PM 0
Share

So I used a Polygon Collider for my Player and a Box Collider for the 2D GameObject that the Player's Polygon Collider is supposed to pull towards the Player, but I ran into some problems. I did everything you said step by step. So the Players Polygon Collider(that is a rectangular shape) that reaches out from the Players right side. Ins$$anonymous$$d of pulling the 2D GameObject towards the Player when it's in the Players Polygon Collider, the 2D GameObject Box Collider collides with the Players Polygon Collider, now let me explain the position of the Player and 2D GameObject. Now the Player and GameObject are on the middle of a platform, so the Player is on the left side of the Scene screen and the GameObject is on the right, okay now when the Player's Polygon Collider collides with the 2D GameObject Box Collider the Player is instantly moved to the right by 20 and moved up by 25, and I know it has something to do with this line of code, Rigid.AddForce(new Vector2(20, 25)); .I then decided to try to figure out what happened, so I change the Rigid.AddForce(new Vector2(20, 25)); to Rigid.AddForce(new Vector2(-1, 0)); .Now when the colliders collide it pushes the Player back while leaving the the 2D GameObject in the same position, then I tried Rigid.AddForce(new Vector2(0, 1)); even when the Player Polygon Collider isn't colliding with the 2D GameObject Box Collider it forces the player into the air, but he eventually comes back down but the Player falls through the platform.

avatar image sadowlight123 DiamondMC102 · Aug 13, 2017 at 05:21 AM 0
Share

hello , did you make sure that the script is attached to the movable object and not to the player? Please let me know what happens

avatar image DiamondMC102 sadowlight123 · Aug 13, 2017 at 06:31 AM 0
Share

I just saw your reply and fixed it, but there still are a couple of problems.

  • One, I want the Player to be able to pull the moveable object towards him and not push them away from the player, and like I said I made the x value negative and it still didn't pull the moveable object towards him. All I want is for the Player's Polygon Collider to pull the moveable object toward the Player so that the Players Box Collider and the moveable object Box Collider collide, and for the Polygon Collider to stop adding force if the Player and moveable object Box Colliders collide.

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

74 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

Related Questions

How do I add force to a RigidBody2D? 2 Answers

Adding force to mouse position adds force relative to screen center 0 Answers

Two Polygon2D Collider do not Collide with Each Other (Solved) 2 Answers

Sprites not recognized as whole-spinning in different directions 0 Answers

2D Detect collisions of a 2D block only on left/right (not top/bottom) 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