- Home /
Making the player change his movement when it hit an object with collider
Hi , I wrote this code to make my player walk the opposite when it hit a wall or something like this but when the player hit the box colliders that is on the game object nothing happens .all help is appreciated
$$anonymous$$ake sure that either your player or the objects have a rigidbody2d (in your case), and some form of collider2d - if you are using a polygon collider, make sure its optimized, otherwise it might be passable, and if you are using a line collider, make sure the lines are facing the right way, otherwise it might be inside-out and also passable to enter, but not to exit
trying to move the player in your collision function wont work cause it only triggers once. the movement needs to happen in Update bbecause it repeats every frame, anyways, simply multiplying your speed by -1 will change direction for you. i would also check to make sure you have your colliders set up properly try changing this part like this:
if(other.transform.name=="collider1"){ print("my collider is working...yay"); speed=speed *-1;}