Camera still moves while I'm moving and don't know why.

So I want my camera to move only when the player isn’t moving and I wrote some code that should prevent that from happening, but it isn’t working. The camera still moves even when the player moves. If anybody can help that would be appreciated.

Code:

public class CameraControllerSG : MonoBehaviour
{

float speed = 20.0f;
public GameObject target = GameObject.FindWithTag(“Player”);
bool motion = true;

// Update is called once per frame
void Update()
{

InMotion(motion);
//want to make sure you can’t move the camera while moving the player
if (Input.GetAxis(“Mouse X”) < 0 && motion == false)
{

transform.RotateAround(target.transform.position, Vector3.down, speed * Time.deltaTime);
}
else if (Input.GetAxis(“Mouse X”) > 0 && motion == false)
{

transform.RotateAround(target.transform.position, Vector3.up, speed * Time.deltaTime);
}
//ignore this

if (Input.GetMouseButtonDown(2))
{
float PlayerArea = 150.0f;
GameObject closestEnemyTarget = FindClosestEnemyWithinRange(PlayerArea);

transform.LookAt(closestEnemyTarget.transform);
}
}

//and this
private GameObject FindClosestEnemyWithinRange(float range)
{
GameObject[ ] gos;
gos = GameObject.FindGameObjectsWithTag(“Enemy”);
GameObject closest = null;
float distance = Mathf.Infinity;
Vector3 position = transform.position;

foreach (GameObject go in gos)
{
Vector3 diff = go.transform.position - position;
float curDistance = diff.sqrMagnitude;

if (curDistance <= range && curDistance < distance)
{
closest = go;
distance = curDistance;
}
}

return closest;
}

public bool InMotion(bool motion)
{
if (Input.GetKey(KeyCode.W))
{
motion = true;
return motion;
}
else if (Input.GetKey(KeyCode.A))
{
motion = true;
return motion;
}
else if (Input.GetKey(KeyCode.S))
{
motion = true;
return motion;
}
else if (Input.GetKey(KeyCode.D))
{
motion = true;
return motion;
}
else
{
motion = false;
return motion;
}
}

}

Try do something like this:

If(Input.GetButton("Horizontal") || Input.GetButton("Vertical))
{
motion = true;
}
else
{
motion = false;
}

For your camera try

If(Input.GetAxis("Mouse X") != 0) && motion = false)
{
**do the thing
}

Don’t do individual buttons, that’s finnicky.

Hope this helps