- Home /
Moving obstacle right and left between the walls, 3D
With this code my obstacle just start moving in one direction and pass through the wall, it doesn't change direction. What to do?
int i = 1;
void Update()
{
transform.Translate(0, 2 * i * Time.deltaTime, 0);
}
private void OnCollisionEnter(UnityEngine.Collision collision)
{
if(collision.collider.tag == "Wall")
i *= -1;
}
I move it on Y axis because obstacle is rotated.
Comment
Answer by Hexcake · Apr 15, 2020 at 05:10 PM
Maybe it helps but not really sure cause I am not an expert. I just wanted to give a hint. The objects which are collided have box colliders? If yes and it still doesn't work then try OnTriggerEnter and check IsTrigger.
Your answer
![](https://koobas.hobune.stream/wayback/20220612231512im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
Moving Obstacle between walls, 3D 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Move Object to location of Trigger? 1 Answer