You want the object to be oriented toward the specified object

Asked 1 years ago, Updated 1 years ago, 41 views

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);

Compare

c# unity3d

2022-09-29 21:37

1 Answers

  • The "forward" of the object is basically in the Z-axis direction, so the cylinder must be rotated in that direction
  • The center of the cylinder is the center of rotation, so you need to change its 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:

To display markers pointing from A to B

+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);
}

To display a marker between A and 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);
}


2022-09-29 21:37

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.