Home > Mobile >  c# unity2d: How can I move an Object and to change the direction when it collides?
c# unity2d: How can I move an Object and to change the direction when it collides?

Time:12-08

The Object moves, but it doesn't change it's direction and i don't know why. I think the problem is with the, but I am not 100% sure.

Thats the code for the object named 'zackenblock':

And thats the code from the tilemap:

Here are some screenshots:

Screenshot of unity and the object

Screenshot of unity and the tilemap

Screenshot of unity and the tilemap2

The object should move to right and when it collides then move left.

It only moves right non-stop.

CodePudding user response:

Use OnCollisionEnter2D because you are making a 2D game

private void OnCollisionEnter2D(Collision2D col)
        {
           
        }

also check out OnCollisionStay2D/exit if the collisions tend to stick .

CodePudding user response:

The major problem with your code is that you are misspelling. Change 'collosin' into 'collision'. And change:

void OnCollisionEnter(Collision collision)

into

void OnCollisionEnter2d(Collision2d collision)

Other than that it seems like your code is fine.

  • Related