• 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 Jdogmaster · Apr 14, 2018 at 04:52 AM · colliderforcepushforcespull

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

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
Best Answer

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.

Comment
Add comment · Show 9 · 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 Jdogmaster · Apr 14, 2018 at 07:05 PM 0
Share

ok so im not sure how to work the settings because rightnow its not doing anything.

avatar image Jdogmaster · Apr 14, 2018 at 07:36 PM 0
Share

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

avatar image Jdogmaster · Apr 14, 2018 at 11:31 PM 0
Share

so how can i make it push all objects in the same dirrection rather than from the way they are facing

avatar image Captain_Pineapple Jdogmaster · Apr 15, 2018 at 09:10 AM 0
Share

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

avatar image Jdogmaster Captain_Pineapple · Apr 15, 2018 at 09:32 PM 0
Share

thanks! tryingit out soon i will let you know how it goes!

avatar image Jdogmaster · Apr 16, 2018 at 10:20 PM 0
Share

how can i make it so that it can only effect objects with the tag of "Untagged"?

avatar image Captain_Pineapple Jdogmaster · Apr 19, 2018 at 11:03 AM 0
Share

just add an if statement in the OnTriggerEnter function:

 if (col.tag == "Untagged") {
             pullObjects.Add (col.gameObject);
         }

avatar image Jdogmaster · Apr 19, 2018 at 01:15 PM 0
Share

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?

Show more comments

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

94 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

Related Questions

pulling objects across collider 0 Answers

adding Up force 0 Answers

pulling in objects one end, and releasing out the other 0 Answers

pulling objects across a collider 0 Answers

How to make Jedi Push and Pull objects (video) 2 Answers

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