- Home /
The question is answered, right answer was accepted
Match position of two object without making one a child of the other
Hi,
I was wondering how I would go about this. Currently I tried this, but this just gives a boatload of errors.
playerbox.transform.Translate = new Vector3(transform.Translate.x, transform.Translate.y, transform.Translate.z);
This script is on object A, and playerbox is the object I want to match position and movement on (but not rotation!).
Any tips or hints in the right direction would be greatly appreciated!
Edit:
Thanks for the great advice! I tried some of it out, and I'm not sure if I'm doing it right, but
playerbox.transform.Translate(transform.position);
moves the playerbox in the correct direction, but at a different speed compared to the original object. I however intend to have the playerbox and the original object to stay at the same speed and location relative to each other, much like as if one was a child of the other.
playerbox.transform.position = transform.position;
Does what I want, but I do not want the playerbox object at the exact same location as the original object.
Any suggestions how I could do this?
Edit2:
I found a workaround which was better then my original design :) Thanks for the help guys, you really pointed me in the right direction!
Answer by Gruffy · Mar 31, 2014 at 12:29 PM
HEY BUD. Okay here...
playerbox.transform.Translate = new Vector3(transform.Translate.x, transform.Translate.y, transform.Translate.z);
you are trying to match the Translate method a little wooozy like. So, if you wanted one transform object to = another you might try writing it like so...
playerbox.transform.Translate(transform.position * time.deltaTime);
What you were trying to say with your code was a good attempt, but Unity needs it in the fashionabove.
All i did was take your code
new Vector3(transform.Translate.x, transform.Translate.y, transform.Translate.z);
and instead of assigning it with "=", it was assigned using the correct syntax for the method structure, which was...
Translate(transform.position);
with the parenthesis(curly brackets) being the important addition and only the need to reference your other transform`s position as the method requires, like you already sussed out there, a Vector3 coordinate.
The only real issue I would have with your code is that your are effectively making your playerBox tranform = the transform of the code that this script is attached to... For ease of use and understanding, perhaps make two public GameObject slots in your script and place both transforms you want to move in there and use there respective cache variable names as prefixes to your transform Translation object and the object you are translating to.. something like....
public Transform playerBox; //accesible through inspector
public Transform target;
void Update()
{
playerbox.transform.Translate(target.transform.position);
}
Anyway. hope that helps some bud. This should solve your issue, but let us know back here if not. Take care Gruffy
Answer by trololo · Mar 31, 2014 at 12:29 PM
Translate is a function, do the same with transform.position
Answer by Simon-Larsen · Mar 31, 2014 at 12:31 PM
You could directly set the position of your GameObject, like so:
playerbox.transform.position = transform.position;
Have to place a comment... Suddenly there were 3 other answers as I posted $$anonymous$$e, lol
Answer by NickP_2 · Mar 31, 2014 at 12:31 PM
Just use:
followingObject.transform.position = leadingObject.transform.position;
If you want to use a more smoother, or some kind of delay follow, you can use Vector.Lerp or Vector3.Slerp:
public float smoothing = 4;
followingObject.transform.position = Vector3.Lerp(followingObject.transform.position, leadingObject.transform.position, Time.deltaTime * smoothing);
This doesn't seem to work if you have fast moving objects. It works fine for rotations, but not for position, for some reason.