- Home /
Object following other object (Parent-like)
Hello, i have this problem: I want to force one object to follow the second object parent-like, but without rotation.
I have this code
#pragma strict
var target:Transform;
var x:float;
var y:float;
var z:float;
function Start () {
}
function Update () {
//takes targets position and modifies it
transform.position=Vector3(
target.position.x-x,
target.position.y-y,
target.position.z-z
);
}
but I don't think it's a good solution for this problem. Any ideas?
Answer by Bunny83 · Apr 04, 2012 at 06:30 PM
This is actually the easiest solution. You might want to use LateUpdate instead of Update() this will ensure that the "parent" object is on it's final position for this frame.
Also your offset should be also a Vector3 so it's much simpler:
var target : Transform;
var offset : Vector3;
function LateUpdate () {
transform.position = target.position - offset;
}
Answer by MrPlayerowy · Dec 05, 2016 at 05:52 PM
I found a way to solve the problem:
public GameObject target;
public Vector3 startPosTarget;
public Vector3 startPosObj;
public Vector3 difference;
void Start()
{
startPosTarget = target.transform.position;
startPosObj = this.transform.position;
}
void LateUpdate () {
difference = target.transform.position - startPosTarget;
this.transform.position = startPosObj + difference;
}
Your answer
Follow this Question
Related Questions
Camera rotation around player while following. 6 Answers
Following another object's position/rotation like parent/child relationship? 4 Answers
Issue with enemies going towards player 2 Answers
make parent of animated game object follow the game object's animation 0 Answers
Problem with making child an object 2 Answers