Raycast 2D Game Object rotating

Hello everyone I am working on a 2d platformer and I have decided to make a basic enemy. I am having a very annoying issue in regards to my raycasting. Everything is working as intended accept for one thing, my Grunt enemy is rotating in place when it locks on to my player. This is also causing the grunts projectiles to rotate as well. I have included the code for my Grunt enemy and I will also include a screenshot of what is going on. So the question is how can I fix it so my enemy doesn’t do that?

Code:
// Name: James Newkirk
//Purpose: This script will handle the various states that the Grunt enemy class will be in.
//Date: 07/24/17

using UnityEngine;
using System.Collections;

using System;
using System.Collections.Generic;

public class GruntStateMachine : MonoBehaviour
{
private enum GruntStates
{
PATROL,
TAKE_DAMAGE,
FIRE
}
//-----------------------------------------------------------------
// Exposed Variables

[SerializeField]
private Transform sightStart;

[SerializeField]
private GruntBasicBullet bullet;

[SerializeField]
private Transform thePlayer;

public bool spotted = false;

//-----------------------------------------------------------------
// Private Variables
Grunt theEnemy;

private GruntStates curState;

RaycastHit2D hitInfo;

int layerMask = 8 << 9;

private bool hasBeenHit = false;

private float trackTime = 1.0f;

Dictionary<GruntStates, Action> fsm = new Dictionary<GruntStates, Action>();    
//-----------------------------------------------------------------

// GameObject Functions


//-----------------------------------------------------------------

// Use this for initialization
void Start ()
{
    theEnemy = GetComponent<Grunt>();

    layerMask = ~layerMask;

    fsm.Add(GruntStates.PATROL, PatrolState);
    fsm.Add(GruntStates.FIRE, FiringState);
    fsm.Add(GruntStates.TAKE_DAMAGE, DamageState);

    SetState(GruntStates.PATROL);
}

// Update is called once per frame
void Update ()
{
    fsm[curState].Invoke();
    //Vision();
}

void Vision()
{
    Debug.DrawLine(sightStart.position, thePlayer.position, Color.red);
    spotted = Physics2D.Linecast(sightStart.position, thePlayer.position, 1 << LayerMask.NameToLayer("Player"));

    if(spotted == true)
    {
        SetState(GruntStates.FIRE);
        Debug.Log("I should fire!");
    }
    else
    {
        SetState(GruntStates.PATROL);
    }
}

//----------------------------------------------------------------
// Helper Functions
void OnTriggerEnter2D(Collider2D col)
{
    if(col.tag == "Player Projectile")
    {
        Debug.Log("Hit!");

        SetState(GruntStates.TAKE_DAMAGE);
    }
}

void Patrol()
{
    //enemy movement goes here
}

//----------------------------------------------------------------
// State Functions

void PatrolState()
{
    Patrol();
    Vision();
}

void FiringState()
{
    trackTime += Time.deltaTime;
    Vision();
    Debug.Log("In firing state!");
    transform.LookAt(thePlayer);

    if(trackTime >=2)
    {
        Instantiate(bullet, sightStart.position, Quaternion.Euler(90, 90, 90));

        trackTime = 0;
    }
}

void DamageState()
{
    theEnemy.ColorTimeIncrease();

    if(hasBeenHit == false)
    {
        theEnemy.TakeDamage();

        hasBeenHit = true;

        gameObject.GetComponent<Renderer>().material.color = Color.red;
    }
    if(theEnemy.GetGruntHealth() == 0)
    {
        Destroy(gameObject);
    }

    else if(theEnemy.ColorTimer () >= theEnemy.ChangeGray())
    {
        gameObject.GetComponent<Renderer>().material.color = Color.gray;
        theEnemy.ResetColorTimer();
        hasBeenHit = false;
        SetState(GruntStates.PATROL);
    }
}
void SetState(GruntStates nextState)
{

    if (nextState != curState)
    {
        //Debug.Log(nextState);
        curState = nextState;

    }
}
//----------------------------------------------------------------

}


I believe is may be because of this line:

 transform.LookAt(thePlayer);

You do not want to use a full transform in a 2D game. Instead, just have the grunt look right if the player position is greater than the grunts, and left if the player position is less than the grunts.

If this solved your problem, please accept this answer. If not, feel free to ask any more questions! :slight_smile: