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
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.