Can't find out to keep the rotation using transform.Lookat

The transform to the new location and looking at a target2 does work fine, but when releasing the target2 it goes back to the angle (y) where it was before using the code Lookat.
I’ve need to release the target2 by using a boolean called newLocation cause otherwise it always will look at that target2,
I tried to use some codes in a void Update like
transform.eulerAngles = new Vector3(0, 0, 0);// lock x,y and z axis to zero
and
Target2.rotation = Quaternion.FromToRotation(Vector3.up, transform.forward);
but can’t find the answer to make this work fine, so I deleted this.

using UnityEngine;
using System.Collections;

public class FirstPersonCharacterScript : MonoBehaviour
{
    public Vector3 fPSControler;
    public Transform Target1;
    public Transform Target2;
    bool newLocation = false;
    public int sec2wait = 3;
    public int[] totalSec = new int[3];
    bool collisionActive = false;


    void Start()
    {
        fPSControler = transform.position;

        InvokeRepeating("TimerInvoke", 1, 1);
    }

    void TimerInvoke()
    {
        if (collisionActive == true)
        {
            if (totalSec[0] < sec2wait)
            {
                totalSec[0]++;
            }
            else
            {
                CancelInvoke("TimerInvoke");
            }
        }
    }

    void OnTriggerEnter(Collider col)
    {
        //if FPSControler has collision with an tagged object
        if (col.tag == "FireFXFlameBall01")
        {
            collisionActive = true;
            newLocation = true;
        }
    }

    void OnTriggerStay()
    {
        //if FPSControler has collision with an tagged object
        if (totalSec[0] >= 2)
        {
            //Transform the position to a new position, target is an empty object
            transform.position = Target1.position;// Object attached to target1 in inspector
        }
    }

    void LateUpdate()
    {
        //Rotating to target
        LookAtTarget();
    }

    void LookAtTarget ()
    {
        if (newLocation == true)
        {
            if (totalSec[0] >= 2)
            {
                // Transform the rotation looking at an object
                transform.LookAt(Target2, Vector3.forward);// Object attached to target2 in inspector
            }
           
            if (totalSec[0] >= 4)
            {
                collisionActive = false;
                newLocation = false;
            }
        }
    }
 
}

Maybe it rotates back towards mouse, if some mouselook is used?
I remember something similar happening, when tried to rotate unity character controller by script…

1 Like

Oke, thanks mgear. I’ll try to find out if this is the problem. I’ll come back to it.