- Home /
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;
}
}
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
Thank you so much. I tried implementing this and failed. I'd rather not embarrass myself by posting my efforts...
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
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).
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*
}
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
void Update should become void Start with "rigidbody2D.velocity = movement;" under it. Attach bounce to objects and it bounces.
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220613143944im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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