- Home /
How can I match/align two objects along 1 axis only?
In the diagram above 3 characters (purple, orange & blue) are standing on a box. The dashed white line represents the x axis of the box running the full length of the box. Using OnCollisionStay, I would like to smoothly align (not snap) a character standing anywhere on the box, so that their centre is aligned with the dashed line (centre of the box) but to only align the character along their x axis (transform.right). In other words the character would only adjust their sideways position (x axis) not forward or up. Eg. if the character is left of the line/centre (purple character) they would smoothly move to their right until on the line; if the character is to the right of the line (orange character) they would smoothly move to their left. Therefore at the end of the alignment the character would always be in the position that the blue character is in (ignoring the difference along the z axis).
I'm not concerned with which way the character is facing since I already have a script to rotate them in the correct direction, I'm only concerned with the position. I've tried using Vector3.MoveTowards and Vector3.Lerp but I'm not sure how to only use this for the one axis. Any suggestions for this please?
Answer by robertbu · Oct 31, 2013 at 04:14 AM
As I read through your question, I can interpret what you want in a few different ways. Some simpler, some harder. I'm going to pick a middle of the road solution. It allow for the base box to be at any arbitrary angle on the 'Y', but assumes that it is level with respect to the XZ plane. It also assumes that you might be rotating the character at the same time you are bringing the characters in line.
The solution is to find the closest point on the Vector3.right of the base box to a character. Then the script brings that point up on the 'Y' to the level of the character. This point is then used for a Lerp(). I also added bringing the character into rotational alignment.
Put the following script on each character. Then you need to drag and drop the base cube onto the 'baseBox' variable for each character. Or you could do a simple rewrite and have this script find the base cube using GameObject.Find().
#pragma strict
var baseBox : Transform;
var speed = 2.0;
var rotSpeed = 1.0;
function Update() {
// Find the closest point on the transform.right of the base box
var v3 = transform.position - baseBox.position;
var t = Vector3.Dot(v3, baseBox.right);
var pt = baseBox.position + baseBox.right * t;
// Bring the point up to the level of this game object
pt.y = transform.position.y;
transform.position = Vector3.Lerp(transform.position, pt, speed * Time.deltaTime);
// Rotation
var q = Quaternion.LookRotation(baseBox.right);
transform.rotation = Quaternion.Slerp(transform.rotation, q, rotSpeed * Time.deltaTime);
}
Omg, I just finished my solution and I was about to post it when I saw your answer :O . Haha, I wish I noticed it 20 $$anonymous$$s ago. Anyway my solution is very similar, I ended up using a Vector3.Lerp to adjust the position but also changed the "To" vector so that its y position and x position weren't affected since I needed movement on only one axis. I can see you've also added a slerp for rotation which isn't actually necessary because as I mentioned I've already got the rotation part finished (previous question I posted). Thanks for your help, I'll mark your answer as accepted so you get kudos for your effort ;)
Answer by FirePlantGames · Oct 31, 2013 at 02:35 AM
have you tried(i don't know if this will work): transform.position.x = Vector3.moveTowards * (a float or int variable)?
I don't think you can modify transform.position.x directly, you have to store it as a temp variable first. I'll try that and see if it works.
Your answer
![](https://koobas.hobune.stream/wayback/20220613120954im_/https://answers.unity.com/themes/thub/images/avi.jpg)