I want to generate obj at random places which can move in random speed in X axis.,How to generate a object at random position with varying speed.

Hey Guys,
I am working on something. In this project i want to generate a object at random position which can go left to right continuously. So far I have got this.
This is inside start of a class that generate 50 object on which player can jump on when game loads.

void Start ()
{
	Vector2 pos = transform.position;

	for (int i = 0; i < noOfHolder; i++)
	{	
		pos.x = Random.Range(-3, 3);
		pos.y += Random.Range(1, 5);
		Instantiate(holder, pos, Quaternion.identity);
	}

and the holder object which is attached to it have a method.

updatePosition()
{
	startingPos.x = moveSpeed * Mathf.Sin(Time.time);//*moveSpeed
	transform.position = startingPos;
}

This code can generate multiple objects. but with the same sin speed from left to right.
how can i vary there X speed.
Thank you for answering.

Hi @LAKSHAYMAVIA

The question is bit unclear, but if you want to modify the object positions using sin values, so that your objects don’t move in same sync, you could do something like this in your instantiation:

var h = Instantiate(holder, pos, Quaternion.identity);
h.GetComponent<Holder>().offset = Random.Range(0f,3f);

And something like this in your Holder:

startingPos.x = moveSpeed * Mathf.Sin(Time.time + offset);
transform.position = startingPos;

With offset variable you can create movement similar to this:

123675-20180830-sin-offset.gif

Multiplying (like your line end comment shows) sin result only modifies the movement, but not it’s timing.