Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Tryptex · Jan 23, 2018 at 05:48 PM · unity 5bugphysics2dvector2collision2d

Collision issues with Vector2.Reflect

I am working on a game where I can launch a ball and when it hits an object it can bounce off without being random, but when I tried to do it I ran into an issues where I was getting weird collision feedback: watch this video and see what I'm talking about: https://1drv.ms/v/s!Ai7u0bw9Rsr-hukdwxJ_MwJ6MaIdLA

public float bulletSpeed; public Rigidbody2D rb2d;

  void Start()
  {
      rb2d = rb2d.GetComponent<Rigidbody2D>();
      rb2d.velocity = transform.up * bulletSpeed;
      rb2d.fixedAngle = true;
  }
  void OnCollisionEnter2D(Collision2D other)
  {
      if (other.transform.CompareTag("Wall"))
      {
          CollisionWithWall(other);
      }
      else if (other.transform.CompareTag("Destroy"))
      {
          Destroy(gameObject);
      }
      else if (other.transform.CompareTag("Target"))
      {
          Debug.Log("Hit: " + other.gameObject.tag);
      }
  }

  void CollisionWithWall(Collision2D other)
  {
      //For Reflecting The Bullet
      Vector3 reflectedPosition = Vector3.Reflect(transform.right, other.contacts[0].normal);
      rb2d.velocity = (reflectedPosition).normalized * bulletSpeed;
      //For Rotate The Bullet Towards its velocity
      Vector3 dir = rb2d.velocity;
      float angle = 90 - Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
      rb2d.MoveRotation(angle);
      //transform.eulerAngles = new Vector3(0, 0, angle);
  }

Please Help, I need to get this working correctly and smoothly ASAP

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Bunny83 · Jan 23, 2018 at 06:02 PM

Why do you reflect the local right direction? Shouldn't you reflect your velocity vector? Your custom rotation code seems to mess things up. If you just look at the orientation of your ball / circle the rectangle is perpendicular to the movement direction. However after the first bounce it's rotated kind of arbitrarily. Therefore your velocity and your right vector do not point in the same direction.


edit

The original approach i had of course didn't work as we can't use the current velocity of the rigidbody inside OnCollisionEnter2D since it got already changed due to the collision. You need to know the velocity / movement direction before the collision. For this there are two possible solutions.


First you can simply store the last velocity vector in a variable inside the FixedUpdate callback. That way we know the last velocity and we can reflect it correctly:

 Vector2 lastVelocity;
 private void FixedUpdate()
 {
     lastVelocity = rb2d.velocity;
 }

 void CollisionWithWall(Collision2D other)
 {
     rb2d.velocity = Vector3.Reflect(lastVelocity, other.contacts[0].normal);
 }

If you want to rotate the object's right direction into the movement direction and you want to move the bullet always at the same speed you could do what you originally did. However you have to ensure the rigidbody doesn't rotate on it's own. So you have to freeze the rotation otherwise our direction would mess up on collisions.

 void CollisionWithWall(Collision2D other)
 {
     transform.right = Vector3.Reflect(transform.right, other.contacts[0].normal);
     rb2d.velocity = transform.right * bulletSpeed;
 }
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Tryptex · Jan 23, 2018 at 06:05 PM 0
Share

@Bunny83 I'm a noob, can you tell what I should change in code Please?

avatar image Tryptex · Jan 24, 2018 at 01:39 AM 0
Share

@Bunny83 Where exactly should I put that is my CollisionWithWall(Collision2D other) function? and what else should I remove? because when I replace all my code in my CollisionWithWall(Collision2D other) function my projectile does this: https://1drv.ms/v/s!Ai7u0bw9Rsr-hukm5pXp7RRpFd44yg

P.S sorry about the framerate, the software I'm using was not built to record video so sorry about.

avatar image Bunny83 Tryptex · Jan 24, 2018 at 02:49 AM 0
Share

Well, yes, it was ment to simple replace your old "CollisionWithWall" method. However I just reaslised that you doing the reflecting in "OnCollisionEnter2D". Of course the velocity has already been changed by the physics system due to the collision.


I barely worked with the 2d physics in Unity. However the easiest way to get the object reflect from a surface is to simply use a "bouncy" physics material on your circle collider. Just create a new "Physics $$anonymous$$aterial 2D" from the Assets / Create menu. Set "Friction" to 0 and "Bounciness" to 1. Now you don't need any code at all. The object will naturally bounce off any object it collides with correctly.


However if you really want to calculate the reflecting yourself I'll edit my example in the answer,

avatar image Tryptex Bunny83 · Jan 24, 2018 at 04:10 PM 0
Share

@Bunny83 Thank you for your help and it did work but because I'm a noob can you explain to me how to code works the way it does a little better?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

153 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Is it possible to find the contact point of a circle collider 2D with another collider inside of OnTriggerEnter2D? 1 Answer

Shoot Projectile on the direction of where the gun is facing 1 Answer

Unity 5 just removed all my lightmaps after 7 hours of baking 1 Answer

Unity Asset Prefab Bug ThirdPersonController (left rotating bug / Snapping forward Always) 3 Answers

Unity 5.5.0 Particle System - Emission Module - Rate over Distance bug 6 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges