Unity Community


Results 1 to 8 of 8

  1. Location
    Winter Park, FL
    Posts
    175

    Object Reflecting off a Wall

    Hey everyone! I'm trying to make an object "bounce" off a wall when it collides with it. The object has a rigidbody component (gravity disabled), and a mesh collider. It is always moving on its z axis. I want it to be deflected off the wall just like in an old pong type of game, so not by the built in physics. Take a look at these images and it may become clear as to what I'm talking about:

    Name:  Rocket 1.png
Views: 1833
Size:  43.7 KB

    Name:  Rocket 2.png
Views: 1799
Size:  43.5 KB

    I have no idea where to begin (I'm pretty new to unity) so any help is appreciated!


  2. Location
    Somewhere on this place we know as earth
    Posts
    1,035
    adding a collider and a rigidbody should work on its own
    Currently working on : Paraverse

    On the Backburner : Operation Snowball, Legends of Merlin


  3. Location
    Liverpool, UK
    Posts
    793
    use raycast from the bullet to find the normal of the surface that's going to be hit

    http://unity3d.com/support/documenta...it-normal.html


    Basically if you have a vector v, which represents the object's velocity, and a normalized normal vector n, which is perpendicular to the surface with which the object collides, then the new velocity v' is given by the equation:

    v' = 2 * (v . n) * n - v;

    Where '*' is the scalar multiplication operator, '.' is the dot product of two vectors, and '-' is the subtraction operator for two vectors. v is reflected off of the surface, and gives a reflection vector v' which is used as the new velocity of the object.


    Or in the example you've given, the normal would be (0,-1,0) and if you bullet is traveling upwards at 45 degrees its velocity might be (-2,2,0) and after the reflection it'd be moving at (-2,-2,0)

    But yeah, use colliders and rigid bodies and let the physics engine sort it out. To prevent an object moving on the z-axis you can either attach a physics joint to the object or you can just add a line in function FixedUpdate that sets the z position and velocity to 0. That's very effective at forcing 2D movement. Also use primitive colliders (box, sphere etc) to make sure it's not at an odd angle.
    Last edited by spinaljack; 01-15-2011 at 07:37 PM.
    LinkedIn

    Message me for rates and availability.
    Mechabit Ltd


  4. Location
    Winter Park, FL
    Posts
    175
    Thanks for helping! But I'm having a hard time putting that math into a script. Could you help me out? Also, the physics engine won't work because when the bullet hits the wall, it just slides to the side as it tries to continue to move forward.


  5. Location
    Croatia
    Posts
    102
    In the Mesh collider in the Inspector try giving it a physic material Bouncy.


  6. Location
    Somewhere on this place we know as earth
    Posts
    1,035
    and if the rigidbody has freeze rotation enabled, turn it off
    Currently working on : Paraverse

    On the Backburner : Operation Snowball, Legends of Merlin


  7. Location
    Kitchener, Ontario, Canada
    Posts
    16
    Quote Originally Posted by spinaljack View Post
    use raycast from the bullet to find the normal of the surface that's going to be hit

    http://unity3d.com/support/documenta...it-normal.html


    Basically if you have a vector v, which represents the object's velocity, and a normalized normal vector n, which is perpendicular to the surface with which the object collides, then the new velocity v' is given by the equation:

    v' = 2 * (v . n) * n - v;

    Where '*' is the scalar multiplication operator, '.' is the dot product of two vectors, and '-' is the subtraction operator for two vectors. v is reflected off of the surface, and gives a reflection vector v' which is used as the new velocity of the object.


    Or in the example you've given, the normal would be (0,-1,0) and if you bullet is traveling upwards at 45 degrees its velocity might be (-2,2,0) and after the reflection it'd be moving at (-2,-2,0)

    But yeah, use colliders and rigid bodies and let the physics engine sort it out. To prevent an object moving on the z-axis you can either attach a physics joint to the object or you can just add a line in function FixedUpdate that sets the z position and velocity to 0. That's very effective at forcing 2D movement. Also use primitive colliders (box, sphere etc) to make sure it's not at an odd angle.
    Just wanted to say: This works great. It took me a bit to get it implemented correctly with my current code (had to reprogram how movement was working to be a Vector3 for velocity), but works great.

    As a quick note for anyone trying to use it: I had to multiply the end result by -1 to make it translate correctly, don't know why.

    Here's the code, in case anyone wants to try it out for yourself
    Code:  
    1. var velocity : Vector3; //Our movement velocity
    2. var speed : float; //Speed of object
    3.  
    4.  
    5. function Awake()
    6. {   
    7.     velocity = this.transform.forward; //Placeholder default velocity // Change to whatever you need
    8.     speed = 20;
    9. }
    10.  
    11.  
    12. function Update()
    13. {
    14.     this.transform.position += velocity * Time.deltaTime * speed; //Update our position based on our new-found velocity
    15. }
    16.  
    17.  
    18. function OnCollisionEnter( info : Collision ) //When we run into something
    19. {
    20.     //'Bounce' off surface
    21.     for( var contact : ContactPoint in info.contacts ) //Find collision point
    22.     {
    23.         //Find the BOUNCE of the object
    24.         velocity = 2 * ( Vector3.Dot( velocity, Vector3.Normalize( contact.normal ) ) ) * Vector3.Normalize( contact.normal ) - velocity; //Following formula  v' = 2 * (v . n) * n - v
    25.        
    26.         velocity *= -1; //Had to multiply everything by -1. Don't know why, but it was all backwards.
    27.     }
    28. }


  8. Posts
    28
    Quote Originally Posted by Loftless View Post
    Just wanted to say: This works great. It took me a bit to get it implemented correctly with my current code (had to reprogram how movement was working to be a Vector3 for velocity), but works great.

    As a quick note for anyone trying to use it: I had to multiply the end result by -1 to make it translate correctly, don't know why.

    Here's the code, in case anyone wants to try it out for yourself
    Code:  
    1. var velocity : Vector3; //Our movement velocity
    2. var speed : float; //Speed of object
    3.  
    4.  
    5. function Awake()
    6. {   
    7.     velocity = this.transform.forward; //Placeholder default velocity // Change to whatever you need
    8.     speed = 20;
    9. }
    10.  
    11.  
    12. function Update()
    13. {
    14.     this.transform.position += velocity * Time.deltaTime * speed; //Update our position based on our new-found velocity
    15. }
    16.  
    17.  
    18. function OnCollisionEnter( info : Collision ) //When we run into something
    19. {
    20.     //'Bounce' off surface
    21.     for( var contact : ContactPoint in info.contacts ) //Find collision point
    22.     {
    23.         //Find the BOUNCE of the object
    24.         velocity = 2 * ( Vector3.Dot( velocity, Vector3.Normalize( contact.normal ) ) ) * Vector3.Normalize( contact.normal ) - velocity; //Following formula  v' = 2 * (v . n) * n - v
    25.        
    26.         velocity *= -1; //Had to multiply everything by -1. Don't know why, but it was all backwards.
    27.     }
    28. }
    I was searching around for a solution to do just what this solves... I like this solution, but I found it difficult to visualize the result using the dot product... so I came up with an intuitive solution. Basically, the result needed is a reaction to the impact on a surface normal. So, we need a reflection about the surface normal which is just flipping the velocity vector about the surface normal and correcting the sign to achieve the final solution. Let my solution explain itself rather than me making it more convoluted than it is:

    Code:  
    1. function OnCollisionEnter( info : Collision ) //When we run into something
    2. {
    3.     //'Bounce' off surface
    4.     for( var contact : ContactPoint in info.contacts ) //Find collision point
    5.     {
    6.         //Find the BOUNCE of the object
    7.         velocity = Quaternion.AngleAxis(180, contact.normal) * transform.forward * -1;
    8.     }
    9. }
    Last edited by ScriptGeek; 02-27-2013 at 08:53 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •