- Home /
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
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));
}
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.
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
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
Follow this Question
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