- Home /
Bounce into the air
0
Hi guys
I'm working on a 2D game, and I need a projectile prefab bounce/flap/jump in the middle of the air after the player press the mouse. In this case, while it moves along the X-axis, it is necessary to apply a force inversely proportional to the projectile's angular direction on the Y-axis in relation to its own x-axis line (only when it has a negative angle in relation to the horizontal line). The new velocity should be minimally lower than the previous one, divided by the percentage of "SlowDownFactor." Something like that:
I'm new to Unity, so I don't know exactly which commands to use to apply this inverse force, I've already taken a good look at the documentation, but I haven't been very successful. The code I have so far:
{
[Range( 0, 1 )]
public float SlowDownFactor = 1;
private Rigidbody2D red_rigidbody;
public void ExecuteAirSpecial()
{
red_rigidbody = GetComponent<Rigidbody2D>();
Vector3 redAng;
red_rigidbody.AddRelativeForce(Vector3.Angle(redAng.y, -redAng.y) * SlowDownFactor);
}
}
Answer by Envans · Nov 24, 2020 at 06:35 PM
[Range( 0, 1 )]
public float SlowDownFactor = 1;
private Rigidbody2D red_rigidbody;
public void ExecuteAirSpecial()
{
red_rigidbody = GetComponent<Rigidbody2D>();
red_rigidbody.AddRelativeForce(Vector2.Perpedicular(transform.forward) * SlowDownFactor);
}
try this, it adds a force to the direction perpdicular to the objects forward (normal in your image)
Hi there, thanks for your awsare. I end up going like that:
public class AirSpecialBounce : $$anonymous$$onoBehaviour, IAirSpecial
{
[Range( 0, 1 )]
public float SlowDownFactor = 1;
private Rigidbody2D red_rigidbody;
public void ExecuteAirSpecial()
{
red_rigidbody = GetComponent<Rigidbody2D>();
Vector2 red_velocity = red_rigidbody.velocity;
if (red_velocity.y < 0)
{
red_rigidbody.AddForce(new Vector2(0, -red_velocity.y) / (SlowDownFactor / 100));
}
}
}
Your answer
Follow this Question
Related Questions
Determining the angle of a projectile collision. 2 Answers
Bullet Behavior with bounce without loosing speed when bounce with another objects. 1 Answer
problem with 2d movement down slopes 0 Answers
Disabling diagonal movement in Unity 5 2D? (using C#) 1 Answer
How to make cars rotate towards a waypoint while turning? 0 Answers