I want to put Sphere again and make it look like a bridge connecting Sphere like the top of the image with the thinned Cylinder in the center, so if you look at one Sphere in LookRotation, the Cylinder will not turn to the second and third (change the second argument to forward and right).By default, Cylinder is in the middle state.
Cylinder.transform.rotation=Quaternion.LookRotation (Sphere.position - Cylinder.transform.position);
One way to resolve this is to insert an empty game object into the hierarchy to correct the coordinate system.Here are two implementation examples:
+CylinderRoot...empty game object
+ Cylinder... Position(0,0,2) Rotation(90,0,0)
We use the simple LookAt() instruction in Transform to direct the target.
//at CylinderRoot
public Transform A;
public Transform B;
void Update()
{
transform.position = A.position;
transform.LookAt(B);
}
+CylinderRoot...empty game object
+ Cylinder... Rotation (90,0,0)
In this case, use LookRotation() because the direction is B as seen from A.
//at CylinderRoot
public Transform A;
public Transform B;
void Update()
{
transform.position = Vector3.Lerp(A.position, B.position, 0.5f);
transform.rotation=Quaternion.LookRotation(B.position-A.position);
}
© 2025 OneMinuteCode. All rights reserved.