Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Ochreous · May 13, 2015 at 10:45 PM · c#2dcollisionprojectile

C# Projectile Ricochet After Collision2d

I'm trying to get my projectiles to ricochet after they collide with a gameobject. I tried GetComponent().velocity = -GetComponent().velocity; and GetComponent().AddForce(-GetComponent().velocity * Time.deltaTime); but neither worked. Am I doing something wrong?

 void Update (){
    GetComponent<Rigidbody2D>().velocity = transform.up * speed;
 }
 
 void OnCollisionEnter2D(Collision2D other){
     //GetComponent<Rigidbody2D> = -GetComponent<Rigidbody2D>;
     GetComponent<Rigidbody2D>().AddForce(-GetComponent<Rigidbody2D>().velocity * Time.deltaTime);
     }




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

Answer by xortrox · May 14, 2015 at 10:03 AM

First of all, googling your question title "projectile ricochet unity" brings up some useful articles that explain the matter in mainly 3D but this should be easy to convert into 2D. Altough the unity documentation might be an ven simpler solution: Collision2D

I believe that Collision2D.relativeVelocity might be something you could look in to.

Over to your script:

 GetComponent<Rigidbody2D>().AddForce(-GetComponent<Rigidbody2D>().velocity * Time.deltaTime);

The result of this is that for the time of the frame of the collision only, you add force based on the rigidbody velocity. Basically if your object was moving at a velocity of (2.0f, 2.0f) and your FPS was at 60, you would have a Time.deltaTime value of 1000/60 = 16.67 ms 16.67 / 1000 = 0.016f

So the velocity would end up negative as you've declared it to be, but it would be multiplied by 0.016f, (0.016f * 2.0f) resulting in (-0,032f, -0,032f) as the new velocity.

So depending on your ricochet type (if it should not bounce off the wall based on angles and just ricochet straigth back) you should only have to do this in your script:

 GetComponent<Rigidbody2D>().velocity = -GetComponent<Rigidbody2D>().velocity;
 
 //Or the more optimized version I think
 
 GetComponent<Rigidbody2D>().velocity *= -1.0f;

Also a side note, you should probably reference Rigidbody2D at the start of your script so you don't have every bullet calling GetComponent when they collide, this could lead to some overhead I've heard.

 public Rigidbody2D Rigidbody2D;
 
 void Start()
 {
 
 Rigidbody2D = GetComponent<Rigidbody2D>();
 
 }

Then later you could do something like this:

 void OnCollisionEnter2D(Collision2D other)
 {
 
 Rigidbody2D.velocity *= -1.0f;
 
 }

Comment
Add comment · Show 1 · 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 xortrox · May 14, 2015 at 10:04 AM 0
Share

If you want a bullet that ricochets based on impact angle then this would not be the solution.

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

19 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

Related Questions

destroying gameobject from other script 1 Answer

Bounce when hitting wall 2 Answers

How to Check if any Children of a GameObject are in a specific area.,Check if a position contains a GameObject or children of a GameObject 1 Answer

How to ensure projectiles don't bump into each other 2 Answers

2D collision detection / box intersection WITHOUT physics 2 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