# Need help with transform.position & Mathf.Cos()

Hi
I´m a beginner coder who’s working on his first game
I´ve a Movement method whose purpose is to move an item to the left and right along the x axis.

The method below is a component on a GameObject that without this code, is instantiated at a random position on the x axis.

``````        GameObject newObstacle = Instantiate(obstacles[RandomObstacleCounter], new Vector3(Random.Range(-8,8), obstacleIndex * distanceBetweenObstacles), Quaternion.identity);
``````

The GameObject that is instantiated is a Prefab that has a script associated to it.
If I comment the code below out, the GameObject is instantiated at a random position in the x axis between -8 and 8.

With the code below, the GameObect is always instantiated at 0,0.

``````void Movement1()
``````

{
Vector2 specialItemPosition = transform.position;
specialItemPosition.x = Mathf.Cos(_angle) * 5;

``````transform.position = specialItemPosition;
_angle += Time.deltaTime * _xSpeed;
``````

}

I think my issued is somewhere in the second line…
I´d like Mathf.Cos(_angle) * 5 not necessarily to be oscillate between 0 in the x axis, rather,
I´d like to add a random value (Random.Range) between -8 and 8 (for example).
Help would be appreciated.

using UnityEngine;

``````public class Obstacle : MonoBehaviour
{
public float xSpeed = 1;
private float _min;
private float _max;
private float _angle;
private float _range;

public void Setup(Vector3 vector3, float min, float max)
{
transform.position = vector3;
this._min = min;
this._max = max;
_range = max - min;
float center = (min + max) * 0.5f;
_angle = Mathf.Acos((vector3.x - center) / _range);
}

private void Update()
{
Vector2 position = transform.position;
position.x = Mathf.Cos(_angle) * _range;
transform.position = position;
_angle += Time.deltaTime * xSpeed;

}
}
``````

``````using UnityEngine;

public class Spawner : MonoBehaviour
{
// ....
void SpawnObstacle(/* ... */)
{
Vector3 position = new Vector3(Random.Range(-8f,8f), obstacleIndex * distanceBetweenObstacles);
GameObject newObstacle = Instantiate(obstacles[RandomObstacleCounter], position, Quaternion.identity);
Obstacle obstacle = newObstacle.GetComponent<Obstacle>();
obstacle.Setup(position, -8f, 8f);
}
}
``````