Rotation problem

I have been working on an Action RPG game. Currently I have the movement set up where I click on the ground and an empty gameobject will be placed at that spot. My character will then move to the location of that gameobject, and will rotate to face where ever the object is.

When I first set it up, the character would always change where they are looking to face up when ever they stop. So I added a boolean that would be true when I am moving, and then false when the character stops moving. During the time that the boolean is true, the rotate script is allowed to run.

Now that I have added more script in, the boolean still goes to false after my character stops moving, however the character changes the direction after it stops moving before the boolean turns to false. I have tried moving the script that turns the boolean to false higher up in the update, but that is not working and the character still changes direction.

I am still new at this and going through College to learn more, but is there something I could be doing wrong, or something that I could be doing better than what I am. Here is my script. Sorry for the amount of script. I am not sure what could be relevant or what wouldn’t be relevant.

//location to move to
    public Transform Target;
    Vector3 moveLoc;
    //movement speed
    public float playerSpeed = 5;
    //home location
    public Vector3 playerSpawn;
    //turning speed
    public float turningSpeed = 5;
    //Object Animator
    public Animator anim;
    //are you moving
    public bool isMoving;
    //is enemy targeted
    public bool enemyTargeted;
    //am i attacking an enemy
    public bool playerAttacking;
    //store the target box to switch back to
    public Transform targetBox;
    //ignore the player layer while clicking
    int playerMask = ~(1 << 9);
    //allow attacking if mob is in range
    bool allowAtt;
    
    // Use this for initialization
    void Start () {

        //get initial position of player as spawn point
        playerSpawn = transform.position;
        //set spawn point as first move location
        moveLoc = playerSpawn;

    }
	
	// Update is called once per frame
	void Update () {
        //set the height of the player
        SetTransformY(0.122f);

        

        if (Input.GetButton("Fire1")) {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit, Mathf.Infinity, playerMask))
            {
                //targeting an enemy or not
                if (hit.collider.gameObject.tag == "Enemy")
                {
                    Target = hit.transform;
                    enemyTargeted = true;
                }
                else {
                    //set bool to is moving
                    isMoving = true;
                    enemyTargeted = false;
                    Target = targetBox;
                   
                }
            }
            
        }
        //switch animation back to idle after attacking
        if (Input.GetButtonUp("Fire1"))
        {
           
            turnAttackOff();
            turnIdleOn();
        }

        
        //attacking animation if allowed to
        if (allowAtt == true)
        {
            if (Input.GetButton("Fire1"))
            {
                Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                RaycastHit attacking;
                if (Physics.Raycast(ray, out attacking, Mathf.Infinity, playerMask))
                {
                    if (attacking.collider.tag == "Enemy")
                    {
                        playerAttacking = true;
                        turnWalkOff();
                        turnAttackOn();
                    }
                    else
                    {
                        playerAttacking = false;
                        turnAttackOff();
                        turnIdleOn();
                    }

                }
            }
           
        }

        //play animations depending on action
        if (playerAttacking == false)
        {
            turnIdleOff();
            turnWalkOn();
            //set move location
            moveLoc = Target.transform.position;

        }
        else {
            turnIdleOff();
            turnAttackOn();
        }


        //turns walk animation off once the player stops moving
        if (Mathf.Approximately(gameObject.transform.position.x, Target.transform.position.x))
        {
            isMoving = false;
            turnIdleOn();
            turnWalkOff();

        }

        if (playerAttacking == false) {
            
            //move toward target location
            transform.position = Vector3.MoveTowards(
                transform.position, moveLoc, playerSpeed * Time.deltaTime
                );
        }

        

        if (isMoving == true)
        {
            //rotate to the direction your moving
            var newRotation = Quaternion.LookRotation(Target.transform.position - transform.position).eulerAngles;
            newRotation.x = 0;
            transform.rotation = Quaternion.Euler(newRotation);
        }
    }

    
    //sets the height of the character
    void SetTransformY(float n)
    {
        transform.position = new Vector3(transform.position.x, n, transform.position.z);
    }
    //turns on idle animation
    void turnIdleOn() {
        anim.SetBool("Idle", true);
    }
    //turns off idle animation
    void turnIdleOff() {
        anim.SetBool("Idle", false);
    }
    //turns on walk animation
    void turnWalkOn() {
        anim.SetBool("Walk", true);
    }
    //turns off walk animation
    void turnWalkOff() {
        anim.SetBool("Walk", false);
    }
    //turns on attack animation
    void turnAttackOn() {
        anim.SetTrigger("Attack");
    }
    //turns off attack animation
    void turnAttackOff() {
        anim.ResetTrigger("Attack");
    }

    void OnTriggerStay(Collider other)
    {
        if (other.GetComponent<Collider>().tag == "Enemy") {
            allowAtt = true;
        }

        
    }

    void OnTriggerExit(Collider other) {
        if (other.GetComponent<Collider>().tag == "Enemy")
        {
            allowAtt = false;
        }
    }
}
         var newRotation = Quaternion.LookRotation(Target.transform.position
  • transform.position).eulerAngles;
    newRotation.x = 0;
    transform.rotation = Quaternion.Euler(newRotation);

I don’t have time to check your method, but I think you would benefit from using RotateAround since you are already using quaternions. The method to get the angle you need to use RotateAround is Quaternion.Angle
If that is not the solution you need try changing the newRotation.x line to newRotation.y
This may also achieve the results you are looking for. Since I’m heading right for my other job after I get off work today I will update this later with the entire method you want to use to get RotateAround working. I really think that it’s what you’re looking for.

Ok, I have it working. I changed the whole rotation script to this one here and all is working again.

Vector3 targetPostition = new Vector3(Target.position.x, transform.position.y, Target.position.z);
transform.LookAt(targetPostition);