Pull Object

Hi, Unity Community! I’m trying to create sort of a pulling function between the player, and object, and an object’s original position. Here’s a diagram of what I’m trying to get at:


So, the yellow object starts off in the center at the startPos. When the player gets 140 units away from the startPos, I would like the the object to follow the player, almost as if there is a string the player has that pulls the object as he gets farther, and a string that pulls the object closer to the startPos as the player gets closer. Then, once the player is within the radial distance, the yellow object should be on the startPos as originated.

I sort of outlined the code already, but the only part I’m having trouble with is the pulling and pushing of the object. (BTW this is a 3D game):

var distance : float = 140.0;
private var startPos : Vector3;
private var player : Transform;

function Start(){
    startPos = new Vector3(transform.position.x, 0 , transform.position.z);
    player = GameObject.FindGameObjectWithTag("Player").transform;
}
function Update(){
    if(Vector3.Distance(startPos,player.position) > 140)
        //Object pulls towards player
    else
        //Object is back in original position
}

Any help would be great! :slight_smile:

edit: need to re-read op

So I came up with a probable solution that might work. Imagine a line always drawn between the player and the startPos. What if the object was always between the player and startPos? I tried doing a midpoint formula, but it does not work. Any help?

gameObject.transform.position = (player.transform.position - startPos.transform.position) * 0.5;

I would be using the addForce method of a regid body

You can subtract your object that is pullings pos from the player pos to get the direction vector to figure to use for this method.

Personally I would use a sphere collider set as a trigger to figure out when the player is in range since it is cheaper than find by tag or find since you can use the on trigger enter and exit messages with it.

To find your midpoint you can add numerous Vector3’s then divide by the total amount, so this case should be (player.transform.position + startPos.transform.position) / 2

I thought that too, but I had gotten an error: MissingFieldException: UnityEngine.Vector3.transform

Woops, startPos is already a Vector3 so you don’t need to reference any objects.
(player.transform.position + startPos) / 2

Ah, makes sense. This works great, now the object sort of follows and pulls towards the player, but there’s one small detail left I can’t seem to figure out, but I’m close. Just like the yellow object, imagine a radius of 140 always around the player. Now we have two radius. Now when the player leaves the radius of the startPos(meaning when the edge of the player’s radius meets the center of the startPos radius, how could I keep the object pined on the borders of the player’s radius while keeping the same motion given? I know it might be complex to comprehend, here’s a picture of what I’m getting at:

This is the code I have so far:

var distance : float = 140.0;

private var startPos : Vector3;
private var player : Transform;
function Start(){
    startPos = new Vector3(transform.position.x, 0 , transform.position.z);
    player = GameObject.FindGameObjectWithTag("Player").transform;
}
function Update(){
    if(Vector3.Distance(startPos,player.position) > 140){
        gameObject.transform.position = (player.transform.position + startPos) / 2;
    }
    else
        transform.position = startPos;
}

Thanks for any help and the help given so far! :slight_smile:

So far all I have is this:

transform.position = player.transform.position + Vector3.ClampMagnitude((player.transform.position + startPos) / 2, distance);

This prevents the object from exiting the player’s radius, however, when the player exits the startPos radius, the object still doesn’t land on the edge of the player’s radius. If someone can help me with this, I would have a solved problem and be much grateful for it. Thank you, and HAPPY NEW YEAR! :slight_smile:

I’ve gotten closer to my answer…When I exit the startPos radius, the object hops and stays on the edge of the player’s radius, however its position on the radius is not quite right, i.e.(If I was exiting south east of the startPos radius, the object will be on the borders of the player’s radius in a south east postion, whereas I would like it to be at a north west position) I’ll keep trying, but any more help would be great!
EDIT: Here’s the updated code:

private var startPos : Vector3;
private var player : Transform;
private var centerPt : Vector3;
function Start(){
    startPos = new Vector3(transform.position.x, 0 , transform.position.z);
    player = GameObject.FindGameObjectWithTag("Player").transform;
}
function Update(){
    if(Vector3.Distance(startPos,player.position) > 140){
        var movement = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
        var newPos = transform.position + movement;
        var offset = -newPos - centerPt;
        gameObject.transform.position = (player.transform.position + startPos) / 2;
        transform.position = player.transform.position + -Vector3.ClampMagnitude(player.transform.position - centerPt, distance);
    }
    else
        transform.position = startPos;
}

EDIT: Actually, adding a negative to the Vector3.ClampMagnitude does put the object in the correct cardinal direction I would like for it to be in, but doesn’t sit on the player’s radius until the player is a more than desired distance away from the startPos.

EDIT: Sorry, but I guess this script is working. It’s just when I enter the startPos radius from the south, it does sort of a little jump glitch, however it works fine when I enter and exit through the north end of the startPos radius…I think I can settle for it. Thanks for all of your help! :slight_smile: