简体   繁体   English

如何围绕单个轴旋转 3D 对象?

[英]How to rotate a 3D object around a single axis?

I have a 3D gameobject which I want to move from A to B in a vertical plane.我有一个 3D 游戏对象,我想在垂直平面上从 A 移动到 B。 I sorted out the movement part with:我整理了运动部分:

public Vector3 start;
public Vector3 end;
public GameObject spaceship;
bool flying;

void OnStart(){

    spaceship.transform.position = start;
    flying = false;
}

void Update(){

    if (flying)
        MovingSpaceship();
}

void MovingSpaceship(){

    if (spaceship.transform.position != end)
    {
        spaceship.transform.position = Vector3.MoveTowards(spaceship.transform.position, end, speed * Time.deltaTime);
    }
}

The problem is I want that the game object points to the destination.问题是我希望游戏对象指向目的地。 I found an answer in this post, but it doesn't work for me to give 0 values to the axis I dont want to rotate:我在这篇文章中找到了一个答案,但是给我不想旋转的轴赋予 0 值对我来说不起作用:

Unity transform.LookAt in only one axis unity transform.LookAt 仅在一个轴上

Is there any other options?还有其他选择吗?

Assuming you are moving the game object in the XY plane, being Z the depth which you are not modifying, then you could place somewhere in your code:假设您正在 XY 平面中移动游戏对象,Z 是您没有修改的深度,那么您可以在代码中放置某个位置:

spaceship.transform.LookAt(end, Vector3.back); 

In case you are not moving the spaceship in the XY plan, then you will need to give a different value to worldUp attribute.如果您没有在 XY 计划中移动spaceship ,那么您需要为worldUp属性赋予不同的值。 Doc 文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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