How combine two rotation?

my enemy must have 2 rotations, the first rotation targets the world since my game is based on a spherical world but at the same time it has to rotate the forward where the player is. Tips?

// Update is called once per frame
   void Update ()
    {
        Vector3 dire = (pianeta.position - this.transform.position).normalized;
        float angleZ = Mathf.Atan2(dire.z, dire.x)*Mathf.Rad2Deg;
        Vector3 tar = (target.position - this.transform.position).normalized;
        float angleY = Mathf.Atan(tar.y) * Mathf.Rad2Deg;
        Quaternion dir = Quaternion.FromToRotation(this.transform.right, dire) * this.transform.rotation; // direction player by world
        Quaternion dirToTargetY = Quaternion.Euler(0, angleY, 0); //direction by target position
        this.transform.rotation = dir;
       
   }

Use transform.localRotation for aiming at the player

ok, but how can I combine the 2 rotations?

  Vector3 tar = (target.position - this.transform.position).normalized;
        float angleY = Mathf.Atan2(tar.y,tar.z) * Mathf.Rad2Deg;
        Quaternion dir = Quaternion.FromToRotation(this.transform.up, dire) * this.transform.rotation; // direction player by world
        this.transform.localRotation = Quaternion.Euler(0,angleY,0);
       // this.transform.rotation = dir;

Math on a spherical surface is frequently going to get hairy.

But in this case, it’s maybe not too bad…use Quaternion.LookRotation to calculate the rotation that faces the target, and for the “up” parameter, specify the normal to the sphere at that point.

1 Like