- Home /
Help I can not understand how to do it with joints
There are 3 objects, one of them always stands in place of the second (A) can only move along one axis, how to do that when the object (A) moves, then the object in its middle changed its position as shown in the photo !
Answer by WarmedxMints · Feb 27, 2019 at 05:33 PM
If you want to use joints, you will need rigidbodys on the object. However, if you just want to fake it, you can simple calc a vector and rotation between the objects. If you put this script on the object in the middle and populate ObjectA and ObjectB with the left and right cubes, it will make the object in the middle face object b on it's Z axis.
using UnityEngine;
public class PositionBetweenObjects : MonoBehaviour
{
public Transform ObjectA, ObjectB;
private void Update()
{
//Get the center point between the objects
var pos = (ObjectA.position + ObjectB.position) / 2;
//Get rotation between objects
//This calc may have to be altered depend on your setup as it will set the Z direction of the object towards objectB
var rot = Quaternion.LookRotation(ObjectB.position - ObjectA.position, transform.up);
transform.position = pos;
transform.rotation = rot;
}
}
Thanks for taking the time to help! Everything is good, only an object in the middle automatically turned 90 degrees along the Y axis
Then change transform.up to transform.right or -transform.right. As I noted in the comments, the rotation may need adjusting depending on how your models are set up.
so far nothing has changed! I think is (down, left) only when I am writing an error vibrating to me
Answer by ibobtouch · Feb 27, 2019 at 05:21 PM
Try this, put a rigidbody component on your middle object, then on the two others cube, put a configurable joint component. On this component there is a "connected body", drag on your middle object. Set the X,Y,Z Motion to "Locked".
Thank you tried, only 2 cubes in the process but the object in the middle just stands still