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 Toonda_Byood · Apr 30, 2014 at 07:09 PM · collisiondirectionbounce

How to make my moving object bounce

I have a script for movement, which i copied from a tutorial - it works great for my project except that i need the projectile to change direction upon hitting another rigidbody; how can i add this?

the code:

 using UnityEngine;
 
 /// <summary>
 /// Simply moves the current game object
 /// </summary>
 public class MoveScript : MonoBehaviour
 {
     // 1 - Designer variables
     
     /// <summary>
     /// Object speed
     /// </summary>
     public Vector2 speed = new Vector2(10, 10);
     
     /// <summary>
     /// Moving direction
     /// </summary>
     public Vector2 direction = new Vector2(-1, 0);
     
     private Vector2 movement;
     
     void Update()
     {
         // 2 - Movement
         movement = new Vector2(
             speed.x * direction.x,
             speed.y * direction.y);
     }
     
     void FixedUpdate()
     {
         // Apply movement to the rigidbody
         rigidbody2D.velocity = movement;
     }
 }
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

2 Replies

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

Answer by poncho · Apr 30, 2014 at 07:34 PM

get the collision object, the colision vector, and use the Vector3.Reflect using incoming vector and the normal of the collision, this will be a 100% elastic collision, you could use a factor to make it bounce less

Comment
Add comment · Show 6 · 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 Toonda_Byood · May 01, 2014 at 01:30 AM 0
Share

Thank you so much. I tried implementing this and failed. I'd rather not embarrass myself by posting my efforts...

avatar image poncho · May 01, 2014 at 01:07 PM 0
Share

i just made this code

 void OnCollisionEnter(Collision other)
     {
         //Check if you have to be specific to an object to bounce
         this.transform.rigidbody.velocity = Vector3.Reflect(other.relativeVelocity*-1, other.contacts[0].normal );
 
     }

be sure the 2 objects have a rigid body good luck and happy coding

avatar image Toonda_Byood · May 01, 2014 at 06:09 PM 0
Share

thank you for the help!

i am experimenting with the code but it is thus far giving me the same problem as i had before; that the object collides, bounces, and momentarily changes direction but almost immediately reverts back to its original path. I believe this is due to:

 void FixedUpdate()
 {
 // Apply movement to the rigidbody
 rigidbody2D.velocity = movement;
 }

since when it refreshes it goes to movement, ignoring the override you had posted for me (unless i improperly implemented it).

avatar image Toonda_Byood Toonda_Byood · May 01, 2014 at 09:18 PM 0
Share

could someone help to figure out how to put this in script:

 void FixedUpdate()
 {
 
 **while rigidbody2D is in motion**
       *continue that motion*
 **unless rigidbody 2D hits something**
       *then transform that motion into bounced direction*
 
 }
avatar image poncho · May 02, 2014 at 01:44 PM 0
Share

I moved your answers to comment(answers are used to answer the question, and comments to expand/debate/follow up the answer), I really recommend any basic program$$anonymous$$g course, also you can try commenting your fixedUpdate code to see how it reacts, good luck

avatar image Toonda_Byood · May 24, 2014 at 07:07 AM 0
Share

void Update should become void Start with "rigidbody2D.velocity = movement;" under it. Attach bounce to objects and it bounces.

avatar image
0

Answer by tanz94 · Dec 29, 2014 at 04:41 PM

Conserve angular momentum before and after collision.https://www.youtube.com/watch?v=RnFAQnm32M8 follow these videos for better understanding.

Comment
Add comment · 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

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

22 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

Related Questions

Moving a ball in the opposite direction of where it hits 1 Answer

How to make Sprite not Bounce on Collision (2D) 6 Answers

Why Rigidbody collide with wall passes through partially? 0 Answers

Ball bounce problem 1 Answer

Moving Randomly 1 Answer


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