Kristian M. Begović
Kristian M. Begović

Reputation: 13

Unity bounce and move

I'm working on a game similar to golf. My situation is this:

I have colliders like martinellas, so their behavior is to bounce ball and keep the same amount of speed.

Well, since I added some abilities, one ability is that my ball transforms into a wheel and goes only in forward direction at higher speed. When it collides with martinellas, it should bounce by Y axis only (in air) and stop movement as well. I managed to do it by freezing the positions.

Looks good for now, but I have a problem: when the ball touches the ground, it collides again, because it goes to the first position of collision. It makes sense.

Now, I need help on how to only bounce in the air, and little bit backwards, so it doesn't collide again (without freezing the positions, I guess).

So, here I added a picture to understand it easier.

enter image description here

1st and 2nd scenario works now.

Now I need the 4th scenario.

The 3rd scenario shows how the ball is dropping by default: it drops down to position of ball in 2nd scenario, which makes collision again. I need the ball to drop down by changed direction, not much, only a little bit to evade the collision.

Upvotes: 0

Views: 652

Answers (1)

Syed Mohib Uddin
Syed Mohib Uddin

Reputation: 716

You can interpolate between y-axis between fixed position.

Vector3 interpolatedPosition = Vector3.Lerp(
new Vector3(transform.position.x, 0, transform.position.z),
new Vector3(transform.position.x, 5, transform.position.z), Time.deltaTime * 5);

transform.position = interpolatedPosition;

Upvotes: 0

Related Questions