Transform.rotateAround() stops working when adjusting the new rotation.

Hi, I have a plane and I’m trying to tilt it while it rotates, but instead of rotating around, it goes in a straight line. If i remove line 63 64
Quaternion overWrite = Quaternion.Euler(transform.rotation.x, transform.rotation.y, -40f);
transform.rotation = overWrite;
it works. But It doesnt become tilted.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class attackPlaneMovement : MonoBehaviour
{
    [SerializeField] float baseTimer = 0.5f;
    public Transform bombLoc;
    public GameObject bomb;
    Transform enemy;
    Vector3 targetVec;
    Vector3 targetLoc;
    public Transform rotationLoc;
    public float rotationSpeed = 300.0f;  // Adjust the speed of rotation.       // Adjust the radius of the circle.
    private Vector3 center;             // Center of the circular path.
    private float angle = 0f;
    float rotationAmount;
    bool isTurning;


    float timer;
    public float speed = 5f;
    Transform myEnemy;
    void Start()
    {
        Quaternion overWrite = Quaternion.Euler(transform.rotation.x, transform.rotation.y, -40f);
        transform.rotation = overWrite;
    }

    // Update is called once per frame
    void Update()
    {
        timer += Time.deltaTime;
        if (myEnemy == null) idleMovement();
        if (timer >= baseTimer)
        {
            timer = 0f;
            myEnemy = FindTarget();
            if (myEnemy == null)
            {
               
                return;
            }

            movePlane(myEnemy);

        }
       
    }



    void movePlane(Transform enemy)
    {
        Debug.Log("moved");
        Vector3 dir = (enemy.position - transform.position).normalized;
        transform.position += Time.deltaTime * speed * dir;
    }

    void idleMovement()
    {
        transform.RotateAround(rotationLoc.position, Vector3.up, rotationSpeed);
        Quaternion overWrite = Quaternion.Euler(transform.rotation.x, transform.rotation.y, -40f);
        transform.rotation = overWrite;
    }
    void turn()
    {
        if (!isTurning) return;


        Vector3 mec = FindTarget().position;
        Vector3 hey = mec;
        mec.y = transform.position.y;
        Vector3 vec = mec - transform.position;
        mec += vec.normalized * 200f;
        targetLoc = mec;
        vec = mec - hey;
        vec.y = 0;
        targetVec = vec;


        float rotationSpeed = 40f * Time.deltaTime;
        transform.RotateAround(rotationLoc.position, Vector3.up, rotationSpeed);
        rotationAmount += rotationSpeed;
        if (Quaternion.Angle(transform.rotation, Quaternion.LookRotation(targetVec)) <= 1f)
        {
            isTurning = false;
            rotationAmount = 0f;
            enemy = null;
        }
    }
 

    Transform FindTarget()
    {
        Transform Target = null;
        float CurrentDistance = float.MaxValue;
        foreach (GameObject obj in getObjects.allEnemies)
        {
          
            float distance = Vector3.Distance(transform.position, obj.transform.position);
            if (distance <= CurrentDistance)
            {
                Target = obj.transform;
                CurrentDistance = distance;

            }






        }

        return Target;

    }
}

Yeah this is quite wrong. You can’t use the xyzw parts of a Quaternion as if they were Euler angles. They are not Euler angles.

3 Likes

After 5 hours of continuous coding, my brain turned to potato. I solved it by rotating the child object and made adjustments using Euler angles. Thanks