Hi everyone,
I have a problem regarding an instantiated prefab. I spawn an object and get it to move, but it only moves upwards. I want it to move downwards. Here is the code:
int Speed = 5;
void SpawnObject()
{
GameObject ob= Instantiate(Prefab) as GameObject;
ob.transform.position = new Vector2(xSpawnPos, ySpawnPos);
ob.GetComponent<Rigidbody2D>().velocity = new Vector3(0, Speed, 0);
}
The “SpawnObject()”-Function is called every 3 three seconds to spawn obstacles. I want the objects to move from the upperscreen straight down. With the above Code the object is moving up, which is the opposite direction. I tried just putting a “minus” before “Speed”, but then they don’t move at all and get spawned at the same location.
The prefab consists of three Rigidbody2Ds which are group to one Gameobject.
I tried it alternativaley with MovePosition, but also failed (not moving at all, only spawning):
ob.GetComponent<Rigidbody2D>().MovePosition(new Vector3(0, 1, 0) * Speed* Time.deltaTime);
Can someone help? Which option do you usually prefer?
Thank you!