- Home /
Direction isn't changing after multiplying it *-1
Hello, i'm trying to make a cube move between 2 walls, everytime it hits a wall the variable that's changing its x position gets multiplied by -1 so the direction change, but this isn't working
This is the code attached to the walls(BoxColliders) #pragma strict var cube1 : Transform; var forceDirection = -2;
function OnCollisionEnter(collision : Collision) {
Debug.Log(forceDirection * -1);
forceDirection = forceDirection * -1;
}
function Update () {
cube1.transform.Translate(forceDirection * Time.deltaTime, 0, 0);
}
This is a link to see what's happening, btw, the Debug.log is giving a forceDirection * -1 value
neoxads.com/sohaib/Test/Test.html
Thank you for reading, i'm looking forward for answers
So OnCollisionEnter does get triggered? ...and only once when it hits the wall? You could also to debug log forceDirection right before you use translate. It would cause a lot of spam in your console but you should be able to see if it's anything related to the forceDirection variable.
Is your Debug.Log outputting just once or is it outputting multiple times?