# Context

I'm here today to figure out how can I have an object to fall down after colliding to another object. In my case, I'm remaking Flappy Bird and trying to reapply some of the physics in place from the original to mine as a fun and learning project.

Yesterday, I came to here and a friendly user helped me out with my problem (post about rotating an object on specific angle while falling). It did wonders and wanted to reapply the same concept again with the methods used previously. Obviously, not the exact same ones, but grab and test out what works the best.

Now, this is what I have at this moment and what I'm trying to replicate

# Problem

As you saw, what I'm trying to replicate is the falling bird from my current height to 0 y-axis in constant speed (keyword "speed"). Currently what I have, is that I Clamp my current y-pos right before I collide and go to 0 (min: 0.0f, max: 0.001f). Which, in my opinion, it is a temporary solution I got, but not suitable for the actual task. I have read more into Lerp and SmoothStep, but I think it will not be constant speed for what I'm achieving. So, instead, I am putting an eye into MoveToward() method from my Bird vector to the floor. MoveToward() moves your object a to move towards to another object b. In my case, it is bird to floor. I tried to use it, but it does not do the trick I was hoping to do it or not using it well enough. (source in how I got into this point)

For now, let's stick from what I have currently from the GIF example I have provided and share the code

# Code

private bool gameOver = false;
private Vector3 velocity;
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("Grass_floor") || collision.gameObject.CompareTag("Pipe"))
{
HitAndDie();
gameover = true;
}
}

private void HitAndDie()
{
// velocity = Vector3.down;
// velocity.y +=  my_gravity * Time.deltaTime; //I need my Time.deltaTime() in here, but not quite sure if I'm using it correctly.
float fallPosition = Mathf.Clamp(transform.position.y, 0.0f, 0.001f);
transform.localPosition = new Vector3(transform.position.x, fallPosition, transform.position.z); //Here I'm just basically want to give a new position to my object from the given clamp.
}


# Explanation

With this code, basically my object bird collides to a another object and it results to go to the floor thanks to the Clamping. Now, once the bird hits, the gameover switches to true and therefore, the gameloop Update() is done. I feel like before triggering my gameover to true, I need to make the bird to fall first to the floor and let gameover turn to true.

To get it down as a falling object before the gameover turn true is what I'm trying to achieve here. So that is why I was looking into MoveTowards() method, which is what I'm picturing to give me results.

If you have questions or something to point out, let me know so I can clarify and potentially edit the post a bit more.

• Did you review previous Q&A such as Transforming Position in y-axis (going down) in Unity or Moving a GameObject to a position, waiting, and then moving it again? Oct 24, 2022 at 18:48
• I haven't yet, I will take a look into it and reflect with it. Now that I'm reading, I will have to get coroutines, which is something I want to avoid due to CPU usage and go through in a for-loop for something simple. Also, while trying to use the Lerp method, I will have to stop my objects from moving left (my pipes/tubes) so they stay where they are after colliding to one. Oct 25, 2022 at 9:03
• I think this is a good example of what I'm trying to aim for but with MoveTowards or Lerp methods Oct 25, 2022 at 9:30

I solved my issue! I was overthinking in my solution that I forgot to have a condition to check when my object collides to 0 pos y-axis. So all of my approaches were just me overthinking in complicated solutions; but that is fine because I have an understanding of how things are working out.

So, what I have change is to add a limit of a certain vector position and have a velocity that includes gravity speed. This is all being checked in my Update() because it is where I'm checking if it is gameOver or not, and a flag whenever I hit the collision.

# Code

private bool gameOver = false;
private Vector3 velocity;
public Transform target; //I give the floor object to my main bird
public bool hit = false;

void Update()
{
StartSpawn(); //Not related to the problem, but here I start with a Time.timeScale to 0
if (!gameOver)
{
FallSpeedAngle(); //Not related to the problem
PlayersAction(); //Not related to the problem
if (transform.position.y > upperBound)
transform.position = new Vector3(transform.position.x, upperBound, transform.position.z);
}
else //HERE is where the problem gets solved
{
if (transform.position.y >= target.position.y)
{
velocity.y +=  my_gravity * Time.deltaTime;
transform.position += velocity * Time.deltaTime;
}
GameOver();
}
}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("Grass_floor") || collision.gameObject.CompareTag("Pipe"))
{
HitAndDie();
GameOver();
}
}

private void HitAndDie()
{
if (hit == false)
{
hit = true;
sound_hit.Play();
sound_die.Play();
}
Time.timeScale = 1;
}
private void GameOver()
{
gameOver = true;
Debug.Log("Game Over!");
}


# Result

I will share this code to my gist if you want to check it out with more details.

Thanks to Sebastian and DMGregory for pointing me out on the right direction (and my peer!)

In your code i see 1 posibly issue im not 100% sure that is for this that your code isn´t working but could be a posibility.You are makin the .Clamp to a float that make reference to the worldposition (transform.position) but then you are modifying the local position that isn´t the same.

Basicaly i create a new position that it´s in the same position in x and z but in the y axis u put Time.deltatime (this is basicly the time between frames that is runing on your pc, when u are making movement in your code u always want to use this instead of your 0.001f constant (time.deltatime is like a very small float). More here https://docs.unity3d.com/ScriptReference/Time-deltaTime.html

This is other posibility to solve the problem but in this case im using transform.postion insted of transform.localPosition. The diference is basicly that transform.position make reference to the global position and not just the parent object. And again instead of using a constant like 0.01f use time.deltatime that is better when u whant to make movement codes.

With the .clamp metod it´s hard to reach the exact 0 y position ,you probaly will reach like 0,000000001 but never 0. So to trigger your game over i like most the first metod because you can triger like this:

Hope it was usefull.

• Not quite, but I think we both have the same idea in the approach. It is still doing that instant drop after colliding, which is what I want to replace with a smooth constant fall effect. Like when I drop a ball from 10ft tall, I'm expecting the ball to fall at some speed each frame of a second. I feel I must add a function to get the effect I want with the Time.deltaTime method. Oct 25, 2022 at 8:02
• Can you please post code in text form instead of as screenshots? That makes it easier for OP to just copy&paste your solution. See this guide on stack exchange meta for more information on how to properly format code. Oct 25, 2022 at 9:10