- Home /
Move into direction of another object
Hello guys.
I want to move my object in the direction of another object. I tried this one:
moveDir = new Vector3(target) - new Vector3(actualPos);
moveDir = moveDir.normalized;
gameObject.transform.position += moveDir;
And it seems to work fine, but only if my object is directed forward (rotation 0,0,0). When it's directed backward (rotation 0,180,0) it doesn't work.
How can I do this? Maybe some easier method but everything will be useful. Bests, Marshall.
Answer by robertbu · Feb 25, 2014 at 08:42 PM
Nothing is this code should be impacted by the direction your character is facing assuming that 'target' and 'acutalPos' are being set correctly. Note if this is executed in Update(), you should be using Time.deltaTime. An alternate code fragment (I'm assuming C# since you are using the 'new' operator):
Vector3 moveDir = (target.position - transform.position).normalized;
transform.position += moveDir * speed * Time.deltaTime;
Note here that 'target' is the transform of the target object. Typically you would declare it at the top of the file like (C#):
public Transform target;
And you would initialize it either by dragging and dropping in the Inspector, or by using GameObject.Find() in start.
I'm not using Time.deltaTime cuz it's one frame of move it.
function Update() {
if(something)
$$anonymous$$oveIt();
}
function $$anonymous$$oveIt() {
var moveDir : Vector3 = (target.position - transform.position).normalized;
transform.position += moveDir;
}
$$anonymous$$y variables "target" and "actualPos" were only examples. So problem is still need help to solve this problem. I'm little sure that I have to count something with transform.rotation main object which I want to move, but don't have any idea, how to count it...
if(rotation.y == 0)then O$$anonymous$$
else NOT O$$anonymous$$
I think that I should equal rotation.y to zero, but only in counting, not in graphic.
There is a lot of room for misunderstanding when you use code with 'examples' or placeholders. Let's use a concrete script that we both can talk about to see where you have a problem:
#pragma strict
var target : Transform;
var moveDist = 0.75;
function Update() {
if (Input.Get$$anonymous$$ouseButtonDown(0)) {
var dir = target.position - transform.position;
if (dir.magnitude <= moveDist) {
transform.position = target.position;
}
else {
transform.position += dir.normalized * dist;
}
}
}
It is expected that 'target' is initialized by dragging and dropping the game object to be used as a target on the 'target' variable in the Inspector.
This code will move a maximum of moveDist units towards the target each time the left mouse button is clicked. Rotation does not matter. Start a new scene, crate a cube, attach this script, create a target game object, drag and drop the target onto 'target' in the inspector, hit play and click the left mouse button.
This is super old, but thank you so much! I just spent an hour or so searching the proper way to move an object in the direction of another object and this is it. No complicated code that I had. No need to detect the direction or rotation. Fantastic answer!