- Home /
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.
Are you detecting 'within distance' via an actual calculation or an OnTrigger? How do you current $$anonymous$$ove things - physics, transform.Translate, CharControl, etc?
I'd like to move it using transform.translate, and I think that using Distance would be easier than using a trigger.
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.
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...
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);
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.
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.
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