# How can I move an obstacole in loop?

Hi guys I’ve got a problem. I’ve those gameobjects with just an edge collider 2D applied. Them are meant to be fireballs, my Idea has been to restart the level once the player hits one of them. Now I just need to move those fireballs from the left to the right and then back to the left in loop. How can I do it? I’ve already tried some methods with the transform class but I’ve been through a several amount of issues, like my fireball moving straight along the X axis with an insanely speed. I’m almost sure it is just a script problem.
Thanks for any help.

Well you probably want to add a rigidbody to the fireball and move the rigidbody using forces?

Then use the OnCollisionEnter function to know when your player is hit.

Either way it really sounds like you need to spend some more time getting familiar with Unity. And it’s not easy to help you when you provide so little information of how you’re doing it. Check out the learning section for some basic tutorials?

https://unity3d.com/learn/tutorials

You’re right, I’m new in unity and I had to add the rigidbody2d, I don’t know why, but I was pretty sure that I hadn’t to do that. My bad. Anyway, I’m just trying to findout wich one is the best script to loop this game object.

Set fireballs gravityscale to zero.
Even if it isn’t the best thing to do, on the fireball script, you can make them move by :

``````public Rigidbody2D _rigidbody;
public float _speed;

void Start()
{
_rigidbody = GetComponent<Rigidbody2D>();
}
void fixedUpdate()
{
_rigidbody.velocity = transform.up * _speed;  //The fireball will go up, change the rotation of the fireball to change the direction the fireball will go to. you can try transform.right, -transform.right   (will go left), -transform.up  (will go down)
//Use the line below to make the fireball change direction. If the ball is going left, it will go right. If it is going up it will go down.
// transform.eulerAngles += new Vector3(0,180,0);
}
``````

Easiest way to do it is using a tween asset. Search Asset Store for “tween”.

It works, pretty easy and intuitive! Thanks

You shouldn’t directly modify the velocity of a rigidbody, but use AddForce(Vector2 force) on it.
I guess you can have weird behaviour later with the code i gave you, and you will have trouble making your fireball move in a more complexe way. (Imagine some kind of magnet trying to attract the fireball, it will add a force to the fireball to attract it, but as you modify the velocity directly, the new force added will disappear)
Plus, in my code, you change the velocity every fixed frame (50 times a second), it isn’t really a good thing. You can set the velocity just once, then modify the velocity only when you need to change the direction.

Your actual fireball behaviour will work as long as your project stays a small project to learn about unity, but if you really need to learn about how Unity Physics work, you should know this code is bad and try to find a better way.