简体   繁体   中英

Rotating an object around a point with the left or right side to the center in Unity3D

As planned, the plane should fly along a given route (there are no problems here). Further, he should deviate to a given point and begin to circle around it. This is where the problems began. If this is done using RotateAround, then the plane flies different sides forward (depending on which side of the screen it flies). Help! =)

transform.RotateAround(point, Vector3.forward, 45 * Time.deltaTime);

在此处输入图像描述

RotateAround works counterclockwise by default. If the plane is near the point in this position, then it flies correctly - with the left wing to the center. 在此处输入图像描述

If so, it flies backwards forward. Sorry for my English )

在此处输入图像描述

If so, or from below, then flies sideways forward.

Further, he should deviate to a given point and begin to circle around it.

Maybe you can try checking for a condition and call Rotate() where centerPoint is the position you want your gameobject to orbit around.

If this is done using RotateAround, then the plane flies different sides forward (depending on which side of the screen it flies). Help! =)

I believe setting rotateClockwise to either true or false will solve this part of your problem.

public float xSpread;
public float zSpread;
public float yOffset;
public Transform centerPoint;

public float rotSpeed;
public bool rotateClockwise;

float timer = 0;
    
// Update is called once per frame
void Update () {
    timer += Time.deltaTime * rotSpeed;
    Rotate();       
}

void Rotate() {
    if(rotateClockwise) {
        float x = -Mathf.Cos(timer) * xSpread;
        float z = Mathf.Sin(timer) * zSpread;
        Vector3 pos = new Vector3(x, yOffset, z);
        transform.position = pos + centerPoint.position;
    } else {
        float x = Mathf.Cos(timer) * xSpread;
        float z = Mathf.Sin(timer) * zSpread;
        Vector3 pos = new Vector3(x, yOffset, z);
        transform.position = pos + centerPoint.position;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM