- Home /
Help in writing a tricky if statement
Hello,
I am creating this 2D game where a ball is move to the left. It will hit a log that is tilted at 45 degrees (or at least the image says so), and when the ball hits it it will either move upwards or downwards depending on the rotation of the log (which the user can modify).
What are the conditions I must write? Here's the code I have.
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.tag == "Ball")
{
//case1
collision.gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2(collision.gameObject.GetComponent<Rigidbody2D>().velocity.y, collision.gameObject.GetComponent<Rigidbody2D>().velocity.x);
//case2
collision.gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2(collision.gameObject.GetComponent<Rigidbody2D>().velocity.y, -collision.gameObject.GetComponent<Rigidbody2D>().velocity.x);
//case3
collision.gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2(-collision.gameObject.GetComponent<Rigidbody2D>().velocity.y, collision.gameObject.GetComponent<Rigidbody2D>().velocity.x);
//case4
collision.gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2(-collision.gameObject.GetComponent<Rigidbody2D>().velocity.y, -collision.gameObject.GetComponent<Rigidbody2D>().velocity.x);
}
}
Answer by Zodiarc · Oct 02, 2018 at 07:39 AM
This seems to be a problem that could be solved easier by the Law of Reflection.
Sorry I screwed up there. It's not the Law of Reflection, but $$anonymous$$omentum with collision. You have the needed mathematics explained in the link. You just need to use them in your code and insert the appropriate values. I can't help you with that, because I also would need to figure out the specifics first. The only thing I can do is to point you in the direction.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
80 tags for one game? 3 Answers
Spawn Prefab at Particle Collision 0 Answers
camera follow obect on pan to z axis 0 Answers
Transform.position.x jumps to high numbers after collision 1 Answer