How to make a object translate alonge an other


I got 2 gameObject in the scene, the first is a capsule and the second is an cylinder.

i have apply some rotations to cylinder and i want the capsule translate along the 3 axes of the of the cylinder.

how to do ?


You can access the axes of the cylinder with the built-in transform variables forward, up, right, etc.

To translate along the +x axis of the cylinder, for instance, you could do the following:

transform.position += cylinderTransform.forward * distanceToTravel;

Good luck.

float distanceToTravelX ,distanceToTravelY,distanceToTravelZ;
distanceToTravelX = 1f;
distanceToTravelY = 1f;
distanceToTravelZ = 1f;
Vector3 dir ;
dir = target.forward * distanceToTravelZ + target.up * distanceToTravelY+ target.right * distanceToTravelX;
transform.Translate(dir * Time.deltaTime);
Is it good ?