C# Lock mousePosition in center

So i have a raycast shooting out from where my mouse is pointing, and i made a simple lock cursor script for the use of menu options later, but that script wont lock the mouse in center. Anyone know how to fix this? (Note: I have been looking in Scripting API but not found a way to script it.)

This is what i was thinking on how the script whould be like:

void Update ()

{
    if (Input.GetKey(KeyCode.Escape))
    Screen.lockCursor = false;
	else
    Screen.lockCursor = true;
      //Move and lock the mouse position to center of the main camera

}

Here is my Raycasting script if needed:

void Update () 
	{
	
		if (Input.GetMouseButtonDown (0))
		
		{
			Ray rayOrigin = Camera.main.ScreenPointToRay (Input.mousePosition);
			RaycastHit hitInfo;

			if(Physics.Raycast (rayOrigin, out hitInfo, distance))
			{
				Debug.Log ("You are casting Ray");
				Debug.DrawLine (rayOrigin.direction, hitInfo.point, Color.blue);

				if(hitInfo.rigidbody !=null)
				{
					hitInfo.rigidbody.AddForceAtPosition (rayOrigin.direction * power,hitInfo.point);
				}


			}

You may want to take a look at the Screen.lockCursor documentation. You want to add an extra boolean value or some other value as a way to make sure the cursor is locked or unlocked. The other question, is if you always want to raycast from center screen, why not use the camera’s forward vector? That’s likely more reliable and is also always the center of the screen.