0

I have an object that, after receiving its respective input, it moves this way:

mov = new Vector3((Input.GetAxis("Horizontal") * vel), 0, 0);
transform.position += mov;

But, I want it to bounce back, once it collides with an object.

I´ve made the procedures already (OnCollisionEnter2D(Collsion2D col){bla bla...}), but I need help with what happens on the collision (bouncing back the object)....

I´ve tried giving the collided object a bouncing material, but it just slows it a bit, my guess is that because of the constant force given by the acceleration.

Greetings.

Dzianis Yafimau
  • 2,034
  • 1
  • 27
  • 38

3 Answers3

1

If you move the object with transform.position what you are doing is basically a "teleport" so it will ignore the bouncing material. If you want it to bounce you have to write the physics code to detect a collision and change the movement or you can do addforce to move the object and it will detect collisions and react automatically.

Tiago
  • 36
  • 3
0

you are teleporting the object at the current time. instead you should use the Rigidbody.addForce this will add a force in the specified direction thus if you do the opposite direction will "bounce" of the object. Another option would be to create a physics material then not bother with the code.

Sean Powell
  • 564
  • 4
  • 20
  • Well, I did't addForce (though I tried it) because I was already using translations and I just learned that it's not good to merge both in a same contexts/ I gave the collision object a sharedMaterial with bouncincess and worked just the way I wanted it. – LifGwaethrakindo Dec 10 '15 at 02:41
0

You are not using materials, right?

See if the content of this post may help you, the OP is using a formula using Raycast and the answer guides him to use the Raycast with Layers Maks:

2D bouncing formula doesn't work properly

There is this one also with fixed angles (like Pong), but it uses material (with values: friction: 0, bounciness: 1): https://gamedev.stackexchange.com/questions/70294/get-gameobject-to-bounce-of-colliders

But if nothing makes sense and you are going crazy and might want to start from zero, there is this official video tutorial on bouncing and sliding in 2D: https://unity3d.com/learn/tutorials/modules/beginner/2d/sliding-bouncing-2d

Community
  • 1
  • 1