- Home /
Game object passes through walls
Hi, I've got an issue that my object passes through walls. Obviously, both have colliders and rigidbodys. I have read that if you are moving the player via its transform.position, then you are going to encounter these issues, as there is no reasonable way for the engine to check if the player will collide until the next physics update... I try to create a 3D game, where an object moves randomly in a camera field. And have created a script for that:
public float timer, timerSpeed, timeToMove, speed; private Vector3 RandomVector(float min, float max) { var x = Random.Range(-220, 220); var y = Random.Range(-80, 90); var z = Random.Range(-65, 50); return new Vector3(x, y, z); } void Update() { timer += Time.deltaTime * timerSpeed; if (timer >= timeToMove) { var rb = GetComponent(); rb.velocity = RandomVector(0f, 5f)*speed; timer = 0f; }
and this works pretty well, but after some time it just leaves a camera field and flies away. No matter if I have built and a box of walls all around it or not... it just doesn't react on them. So there' are 2 ways I assume... 1. Write a new script that will make my object move randomly in the camera field 2. Somehow make my object react to walls
I've been looking for a script that makes my object move randomly, but I have not found any. Could you manage to help me?