Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by Slvr99 · Feb 15, 2014 at 11:39 PM · javascriptmove an object

Moving a GameObject when it's within a distance from another?

How can i move a GameObject child from it's Parent when the Object comes within a certain distance of another Object using JavaScript? I want to move the Object from it's current position, by basically unparenting it from it's Parent, and move it to the other GameObject. Then have it wait for about 0.2 seconds and then move the Object back to it's Parented location and move the other object to the Parent's Origin. An example would be if you have some object, let's say a Cube or Sphere, Parented to the Player, and have the Cube/Sphere go over to the other Object to bring it back to your character and then go back to it's Parented position.

Comment
Add comment · Show 2
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 getyour411 · Feb 15, 2014 at 11:41 PM 0
Share

Are you detecting 'within distance' via an actual calculation or an OnTrigger? How do you current $$anonymous$$ove things - physics, transform.Translate, CharControl, etc?

avatar image Slvr99 · Feb 15, 2014 at 11:45 PM 0
Share

I'd like to move it using transform.translate, and I think that using Distance would be easier than using a trigger.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by robertbu · Feb 16, 2014 at 12:14 AM

There are many open question about what you are asking for. Here is a bit of example code. It goes on the child object. You don't have to unparent the two object in order to solve this problem. 'target' is the object that triggers the movement. As long as the parent object is within 'dist', the child movement will cycle. If you don't want that, you'll have to code come additional state to catch the transition to/from 'distance' from target.

 #pragma strict
  
 public var target : Transform;
 public var dist  = 2.5;
 public var speed = 2.25; 
 
 private var moving = false;
  
 function Update() {
     if (!moving && Vector3.Distance(target.position, transform.position) < dist) {
         MoveIt();
     }
 }
 
 function MoveIt() {
     moving = true;
     
     while (transform.position != target.position) {
         transform.position = Vector3.MoveTowards(transform.position, target.position, speed * Time.deltaTime);
         yield;
     }
 
     yield WaitForSeconds(0.2);
     
     while (transform.localPosition != Vector3.zero) {
         transform.localPosition = Vector3.MoveTowards(transform.localPosition, Vector3.zero, speed * Time.deltaTime);
         yield;
     }
     moving = false;
 }

'MoveIt()' is a co-routine that will complete the cycle of movement. The last while() loop returns the child to the parent. Note how this code uses 'localPosition' rather than 'position.' This allows the child to return to the parent even if the parent moves while the child is moving.

Comment
Add comment · Show 5 · 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 Slvr99 · Feb 16, 2014 at 08:07 AM 0
Share

For some reason, this isn't working for me... I've set the Transform to the GameObject I want it to pick up, but when I move near the object, nothing happens...

avatar image robertbu · Feb 16, 2014 at 08:12 AM 0
Share

This needs to be on the child object, not the parent. In Update() put the following to see how the distance calculation is doing and how it compares to the dist parameter:

 var d = Vector3.Distance(target.position, transform.position);
 Debug.Log(dist+", "+d);
avatar image Slvr99 · Feb 17, 2014 at 11:46 PM 0
Share

Odd, that doesn't seem to do anything either. I added it to Update and the log isn't showing anything... What am I doing wrong? And yes, I am adding this to the Child Object, not the Parent.

EDIT: Well, I figured out why that code doesn't work, and the Distance is updating correctly. I set the appropriate value and when the distance gets below the value i set, nothing happens still.

avatar image robertbu · Feb 18, 2014 at 02:28 AM 0
Share

I don't know what is happening with your project. Here is a sample project using the above script. Start a new project, import the package, hit play, and use the arrow keys to move around. When you get within the specified distance, the sphere will go to the target. The cube is the target.

www.laughingloops.com/Example01.unitypackage

avatar image Slvr99 · Feb 22, 2014 at 12:30 AM 0
Share

I see my problem. I had an animation added to the object, so the animation wasn't allowing him to move. Is there anyway around this? Or do i need to animate the object differently?

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

19 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

Related Questions

Instantiate object to move with other gameObject 0 Answers

How to move a Child independently to another object and back. 1 Answer

How do I find out where a child is with javascript 3 Answers

Trying to move an object between two points 2 Answers

Transform.Translate Does not work correctly 1 Answer


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