- Home /
This question was
closed Jul 05, 2018 at 12:19 AM by
bilginba for the following reason:
Question is off-topic or not relevant
Question by
bilginba · Jul 03, 2018 at 09:30 PM ·
collision2d gamecollision2dcollision issuescollision-error
How to disable circle rolling when it hits collision's corner?
Hello
I have a circle game object that it should move horizontally or vertically. If it hits collision from one axis, it should move only the other axis. For example if my circle hits the object from x-axis, it should move only with y-axis. To make it simply i want it like a pacman movement. However, when i hit my circle to corner of the obstacles (other objects in the scene) it rolls through the scene.
I want it to stop movement at the same axis and disable two axis movement at the same time.
My solution might be wrong. I'm open to every solution i can get. Thank you all.
void FixedUpdate() {
//Movement
float h = Input.GetAxisRaw("Horizontal");
float v = Input.GetAxisRaw("Vertical");
if (Input.GetAxisRaw("Horizontal") > 0f || Input.GetAxisRaw("Horizontal") < 0f) {
GetComponent<Rigidbody2D>().velocity = new Vector2(h, 0) * speed;
}
else if (Input.GetAxisRaw("Vertical") > 0f || Input.GetAxisRaw("Vertical") < 0f) {
GetComponent<Rigidbody2D>().velocity = new Vector2(0, v) * speed;
}
}
void OnCollisionEnter2D(Collision2D collision) {
if (collision.gameObject.tag == "Obstacle") {
GetComponent<Rigidbody2D>().velocity = Vector3.zero;
GetComponent<Rigidbody2D>().angularVelocity = 0;
}
}
Player
playerss.png
(29.3 kB)
enemyss.png
(29.2 kB)
Comment