简体   繁体   中英

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. 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:

Unity transform.LookAt in only one axis

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:

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. Doc

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