- Home /
pulling objects
i was wondering how to make a collider that pulls an object in a direction so usually i can make the collider pull the object to the center of it. but i just want it so if an object enters the collider it will move all the way through it and only stop once it has exited the collider
Answer by Captain_Pineapple · Apr 14, 2018 at 08:55 AM
Hey there,
try the following:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PullScript : MonoBehaviour {
private List<GameObject> pullObjects;
public Vector3 pullDirection;
public float pullSpeed;
void Start () {
pullObjects = new List<GameObject> ();
}
void Update () {
foreach (GameObject obj in pullObjects) {
obj.transform.Translate (Time.deltaTime * pullSpeed * pullDirection);
}
}
public void OnTriggerEnter(Collider col)
{
Debug.Log ("object entered");
pullObjects.Add (col.gameObject);
}
public void OnTriggerExit(Collider col)
{
pullObjects.Remove (col.gameObject);
}
}
Attach this script to an object that should pull others through. You can define a speed and a pullDirection in the editor. Both objects need a trigger Collider! One of them additionaly needs to have a rigidbody attached. Otherwise unity will not register any collision.
ok so im not sure how to work the settings because rightnow its not doing anything.
ok so i figgured it out and it works but the problem is if i have it set to pull objects through it along the z axis it pulls the object depending on how the object is facing so the objects end up going in random dirrections depending on what their z axis is
so how can i make it push all objects in the same dirrection rather than from the way they are facing
$$anonymous$$y bad, apparently i did think this through completly. Currently the translate function translates along a vector relative to the transform of the object to translate.
The thing you need is to change line 16 into:
obj.transform.Translate (Time.deltaTime * pullSpeed * pullDirection,Space.World);
or:
obj.transform.Translate (Time.deltaTime * pullSpeed * pullDirection,transform);
The first option will interpret the translate vector in world-space. The second option will interpret it relatively to the transform of the pulling object.
thanks! tryingit out soon i will let you know how it goes!
how can i make it so that it can only effect objects with the tag of "Untagged"?
just add an if statement in the OnTriggerEnter function:
if (col.tag == "Untagged") {
pullObjects.Add (col.gameObject);
}
Thanks man, one more question, how can I make it so that no matter where an object enters the collider it will just be pulled straight through, not following an x y org axis just wherever it enters the collider it will be pulled to the opposite side from where it entered?
Your answer
![](https://koobas.hobune.stream/wayback/20220612160723im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
pulling in objects one end, and releasing out the other 0 Answers
pulling objects across a collider 0 Answers
pulling objects across collider 0 Answers
adding Up force 0 Answers