Hi, I am currently trying to find a solution to my problem but just can’t seem to get it right. What I want to happen is when the raycast collides with the ground, the player is launched toward a direction based on the angle of the collider/ground.
My issue is that the AddFroce does not seem consistent in always bringing the player to a certain height, and will sometimes shoot the player high into the air and other times not at all. Here is my code to do this:
private void Update()
{
RaycastHit2D hit = Physics2D.Raycast(raycastStartPoint.position, -Vector2.right, 0.001f);
if (hit.collider != null)
{
if (hit.collider.name == "Ground" || hit.collider.name == "LeftWall" || hit.collider.name == "RightWall")
{
if (shouldAddForce == true)
{
direction = -hit.point; // Find the point of collision
shouldAddForce = false; // Does not allow for antoher collision
StartCoroutine(AllowAnotherShield()); // Calls to end no more collisions
objectRigidbody.velocity = Vector2.zero; // Stops the player's velocity so add
// force does not have to counter the
// current velocity
}
}
}
if(shouldAddForce == false) // If we should be adding force
{
AddForce();
}
}
private void AddForce()
{
objectRigidbody.AddRelativeForce(direction * force); /// Adds force at direction
}
private IEnumerator AllowAnotherShield()
{
yield return new WaitForSeconds(seconds); // Waits for seconds
shouldAddForce = true; // We can find another collision
}
}
Here is a youtube video on what my game looks like and you can see sometimes the player flies way high and other times it does not: Bounce Mechanic - YouTube