- Home /
Odd behavior when circle collider is supposed to bounce off wall
Hello. First time posting here. So I have a script written for when a 2D circle collider representing an object is in motion and hits a solid object. In my game the surfaces are only along the X and Y axis since they are boxes. I use 4 raycasts from the center of the object which extend the radius of the ball going up, down, left, and right to detect object collision, since the collider is a trigger. On my trigger enter, these 4 raycasts are sent and if one of them collides, I am able to determine which side of the ball was collided with. If it's the top or bottom, I reverse the Y velocity, and if it's the sides, I reverse the X velocity. This is to simulate a linear bounce off a wall of the ball.
The issue is that when the object collides with a wall, it only seems to work properly if it's travelling a specific direction and hits a specific side. For instance, if the ball is sent at a speed to the right and hits a wall, it will pass right through. However, from the left it will bounce as intended. Is there an error in the way I've written this code that explains this odd behavior? Please examine my code:
public class sword : MonoBehaviour {
public Rigidbody2D rb;
private float spin, ball_radius;
public float speed, trajectory;
public GameObject Player;
Animator anim;
// Use this for initialization
void Start () {
speed = 0.0f;
anim = Player.GetComponent<Animator>();
trajectory = 0.0f;
ball_radius = 0.15f;
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.tag == "player" && speed <= 1)
{
transform.position = new Vector2(-1000, -1000);
anim.SetBool("Sword", true);
Player.GetComponent<movement>().equipped = true;
}
if (collision.tag == "solid")
{
RaycastHit2D hitup = Physics2D.Raycast(transform.position, Vector2.up, ball_radius);
RaycastHit2D hitright = Physics2D.Raycast(transform.position, Vector2.right, ball_radius);
RaycastHit2D hitdown = Physics2D.Raycast(transform.position, Vector2.down, ball_radius);
RaycastHit2D hitleft = Physics2D.Raycast(transform.position, Vector2.left, ball_radius);
if (hitup.collider != null)
{
trajectory = Mathf.Rad2Deg * (Mathf.Atan(-rb.velocity[1] / rb.velocity[0]));
}
if (hitright.collider != null)
{
trajectory = Mathf.Rad2Deg * (Mathf.Atan(rb.velocity[1] / -rb.velocity[0]));
}
if (hitdown.collider != null)
{
trajectory = Mathf.Rad2Deg * (Mathf.Atan(-rb.velocity[1] / rb.velocity[0]));
}
if (hitleft.collider != null)
{
trajectory = Mathf.Rad2Deg * (Mathf.Atan(rb.velocity[1] / -rb.velocity[0]));
}
}
}
private void FixedUpdate()
{
rb.velocity = new Vector2(speed * Mathf.Cos(trajectory * Mathf.Deg2Rad), speed * Mathf.Sin(trajectory * Mathf.Deg2Rad));
speed = speed * 0.93f;
}
}
Note that I have to convert degrees to radians in the final movement calculation because the way my object it moved is based on the Euler rotation of another object. A different script handles setting the objects speed when it travels, in case you are wondering how it begins acceleration in the first place. Thanks for looking at this problem for me.
Answer by Cornelis-de-Jager · Nov 19, 2018 at 01:55 AM
Hi, I assumed it is something to do with how you calculate your trajectory. You are using the same trajectory with for both hitting Up and Down and then again the same for Left and Right. I assume the aTan calculation is returning the same result for both positive and negative values. Might need to test that. however I have made a different solution below without using the math and raycasts:
Vector2 trajectory; // Change this to vectory 2
private void OnTriggerEnter2D(Collider2D collision)
{
Vector2 vec = collision.point - transform.position;
var x = Math.Abs(vec.x);
var y = Math.Abs(vec.y);
if (x > y) {
// Change x direciton
trajectory= new Vector2 (-rb.velocity[0], rb.velocity[1]);
} eles if (y > x){
// Change y direciton
trajectory = new Vector2 (rb.velocity[0], -rb.velocity[1]);
} else {
// Change both
trajectory = new Vector2 (-rb.velocity[0], -rb.velocity[1]);
}
}
private void FixedUpdate()
{
rb.velocity = trajectory * speed;
speed = speed * 0.93f;
}
Thanks for your answer! The code works much better now, however there is still odd behavior happening typically when I try to bounce it at more flat angles. It sometimes bounces the wrong way or phases through the object. I've had to adjust your code a bit. For one, when you set rb.velocity[0] or [1] to be negative or positive, I believe you meant to set trajectory[0] and [1], since doing the velocity causes a very large speedup. I also had to use a raycast in the end, since I am using a trigger and therefore do not have a point of contact. I think the issue has to do with the raycast. Can you suggest an alternative method for finding the contact point if I'm using a trigger? The reason it is a trigger is because I was finding it difficult to remove the unity engine physics I needed to remove from a regular circle collider, like when the ball collided with a non solid object for example. Here is my edited code:
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.tag == "player" && speed <= 1)
{
transform.position = new Vector2(-1000, -1000);
anim.SetBool("Sword", true);
Player.GetComponent<movement>().equipped = true;
}
if (collision.tag == "solid")
{
Debug.Log(trajectory);
hit = Physics2D.CircleCast(transform.position, ball_radius, trajectory);
var x = $$anonymous$$athf.Abs(hit.point[0]);
var y = $$anonymous$$athf.Abs(hit.point[1]);
if (x > y)
{
// Change x direciton
trajectory = new Vector2(-trajectory[0], trajectory[1]);
}
else if (y > x)
{
// Change y direciton
trajectory = new Vector2(trajectory[0], -trajectory[1]);
}
else
{
// Change both
trajectory = new Vector2(-trajectory[0], -trajectory[1]);
}
}
}
private void FixedUpdate()
{
rb.velocity = speed * trajectory;
speed = speed * 0.93f;
}
Hi again. Hope you are still around. I've simplidied the code even more using Unity methods, and now it works about the same as your original answer. Still having those odd issues, can you identify an issue with this code?
public class sword : $$anonymous$$onoBehaviour {
public Rigidbody2D rb;
public CircleCollider2D coll;
private float spin;
public float speed;
public Vector2 trajectory;
public GameObject Player;
Animator anim;
RaycastHit2D hit;
void Start () {
speed = 0.0f;
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.tag == "solid")
{
hit = Physics2D.Raycast(transform.position, trajectory);
trajectory = Vector2.Reflect(trajectory, hit.normal);
}
}
private void FixedUpdate()
{
rb.velocity = speed * trajectory;
speed = speed * 0.93f;
}
}