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
(Mine. I'm doing frame by frame to point out)
(Original concept)
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.