- Home /
Bouncing square stops moving diagonally
In my current weekly project, I've got a square that bounces moves diagonally across the screen after a certain interval. During every game, at least one of these squares stops moving diagonally and instead only moves on the X or Y axis.
I have a physics component that has bounciness at 1 and friction at 0
The object has a rigidbody2d with gravity set to 0 and fixed angle turned on, no other changes.
I've attached the script I've been trying to use most recently to no avail
I've attempted to use Physics.Reflect but that doesn't seem to fix the issue as well.
Note, the issue normally happens when it is pinned between one object and a wall.
public class BouncerObj : MonoBehaviour {
Rigidbody2D rb2d;
void Start()
{
rb2d = transform.GetComponent<Rigidbody2D>();
rb2d.fixedAngle = true;
}
void OnCollisionEnter2D(Collision2D other)
{
if ((other.gameObject.tag == "Player" || other.gameObject.tag == "Enemy") && !gameObject.GetComponent<Collider2D>().isTrigger)
{
gameObject.GetComponent<Collider2D>().isTrigger = true;
}
else if ((other.gameObject.tag == "Friendly" || other.gameObject.tag == "Wall" || other.gameObject.tag == "Bouncer"))
{
Vector2 dir = new Vector2(-transform.position.x,-transform.position.y);
GetComponent<Rigidbody2D>().velocity = dir;
}
}
void TriggerChange()
{
gameObject.GetComponent<Collider2D>().isTrigger = true;
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.tag == "Player")
{
if (MovePlayer.invincible == true)
{ }
else
{
Scores.fShot += 5;
MovePlayer.invincible = true;
}
}
else if ((other.gameObject.tag == "Friendly" || other.gameObject.tag == "Wall" || other.gameObject.tag == "Bouncer") && gameObject.GetComponent<Collider2D>().isTrigger)
{
gameObject.GetComponent<Collider2D>().isTrigger = false;
Invoke("TriggerChange", 0.03f);
}
}
}
Bumping. The week is over so I'm going to start something else, but I'm still curious as to what was causing the angles to change.