- Home /
move till collision
I would like to move my "kraan" down until I hit a box. How to do this?
function OnGUI(){
if(Input.GetKeyDown("g"))
{
Grijp();
}
}
function OnCollisionEnter(collision : Collision) { botst = true; }
function Grijp(){ var max:int = 10; for( var i:int =0;i<max;i++) //while(botst == false) { if(botst == false) {
kraan.transform.position = kraan.transform.position + Vector3.down/3; Debug.Log(botst); } if(botst == true) {
max = i; Debug.Log(botst); }
} }
Answer by Bampf · Apr 18, 2011 at 12:41 PM
Collisions are checked periodically. Your Grijp method will run entirely between those checks. How you solve this depends on what you are trying to achieve.
If you want to find the point at which kraan will collide, and move it there all in one frame, then you could cast rays (or cast a sphere) in the direction of movement to find where the collision point will be. This would replace your while loop.
If you are trying to move the kraan a little each frame, so that the player sees the movement until kraan hits something, then instead of a while loop, you would just move the object once every time Update() or FixedUpdate() is called.
Your answer
![](https://koobas.hobune.stream/wayback/20220613123459im_/https://answers.unity.com/themes/thub/images/avi.jpg)