Unity 2D - How does velocity and bouncy physic material causes the ball to change direction everytime it hits...





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







-1















So, I'm following a youtube tutorial on how to make pong. I understand and learned alot of stuff but there's a point where he made the ball and its movement but he didn't explain it. He used



void Start() 
{
rb.velocty = new Vector2(speed, speed);
}


The ball is also attached with a rigidbody2d, a circle collider with a physic material attached. The physic material has a friction of 0 and bounciness to 1. This somehow made the ball bounce off an object.



Can anyone explain to me how it works?



Thanks alot!










share|improve this question

























  • If you meant to ask how does ball change its direction, this behaviour is handled by Physics Engine on Unity. Gravity also handled by this.

    – darcane
    Jan 4 at 6:56


















-1















So, I'm following a youtube tutorial on how to make pong. I understand and learned alot of stuff but there's a point where he made the ball and its movement but he didn't explain it. He used



void Start() 
{
rb.velocty = new Vector2(speed, speed);
}


The ball is also attached with a rigidbody2d, a circle collider with a physic material attached. The physic material has a friction of 0 and bounciness to 1. This somehow made the ball bounce off an object.



Can anyone explain to me how it works?



Thanks alot!










share|improve this question

























  • If you meant to ask how does ball change its direction, this behaviour is handled by Physics Engine on Unity. Gravity also handled by this.

    – darcane
    Jan 4 at 6:56














-1












-1








-1








So, I'm following a youtube tutorial on how to make pong. I understand and learned alot of stuff but there's a point where he made the ball and its movement but he didn't explain it. He used



void Start() 
{
rb.velocty = new Vector2(speed, speed);
}


The ball is also attached with a rigidbody2d, a circle collider with a physic material attached. The physic material has a friction of 0 and bounciness to 1. This somehow made the ball bounce off an object.



Can anyone explain to me how it works?



Thanks alot!










share|improve this question
















So, I'm following a youtube tutorial on how to make pong. I understand and learned alot of stuff but there's a point where he made the ball and its movement but he didn't explain it. He used



void Start() 
{
rb.velocty = new Vector2(speed, speed);
}


The ball is also attached with a rigidbody2d, a circle collider with a physic material attached. The physic material has a friction of 0 and bounciness to 1. This somehow made the ball bounce off an object.



Can anyone explain to me how it works?



Thanks alot!







unity3d bounce pong






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 4 at 7:59









derHugo

9,87131635




9,87131635










asked Jan 4 at 5:12









Marc2001Marc2001

9818




9818













  • If you meant to ask how does ball change its direction, this behaviour is handled by Physics Engine on Unity. Gravity also handled by this.

    – darcane
    Jan 4 at 6:56



















  • If you meant to ask how does ball change its direction, this behaviour is handled by Physics Engine on Unity. Gravity also handled by this.

    – darcane
    Jan 4 at 6:56

















If you meant to ask how does ball change its direction, this behaviour is handled by Physics Engine on Unity. Gravity also handled by this.

– darcane
Jan 4 at 6:56





If you meant to ask how does ball change its direction, this behaviour is handled by Physics Engine on Unity. Gravity also handled by this.

– darcane
Jan 4 at 6:56












1 Answer
1






active

oldest

votes


















3
















  • The line



    rb.velocity = new Vector2(speed, speed);


    simply sets the initial velocity of the object in x and y direction.



    Since they use x = y = speed in the vector definition the ball will move diagonal in a 45° angle.



    Change those values in order to get different angle or speed.




See Physic Materials:




  • friction = 0 means that the object will not slow down. A value bigger than 0 would slow down the object over time.


  • bounciness 1 means that on collision the object won't loose any speed but instead bounce back with 100% of its velocity at the according angle. -> This is the part changing the direction of the object according to its impact angle etc



This is all handled by Unity's PhysicsEngine itself so you don't have to worry about that.






share|improve this answer


























  • Thanks for the answer! Explained me alot. But can you explain me why velocity does that? For example, a ball floating in the sky with no gravityScale and on "Start", we add a velocity of (0, -5). When we hit play, the ball will fall and touch the ground then, bounces off in the opposite direction instead of doing a rotation of 90 degrees. Can you explain me why this example, it bounces off the opposite direction whereas my question bounces and rotates 90 degrees?

    – Marc2001
    Jan 4 at 12:32













  • The difference is that if one of the two velocity axis is set to 0 it hits a flat surface with impact angle 90° .. as you surely know from natural physics and the reflection law incoming angle = outgoing angle. The same happens if you let a ball drop in nature exactly streight down => it comes back streight up. In the example however the angle was 45° as mentioned so the ball bounces of on a reflected movement vector which is -45° on the surface => the difference in that case is 90°

    – derHugo
    Jan 4 at 12:39













  • I'm terrible with physics but you explained it all. Thank you!

    – Marc2001
    Jan 4 at 22:37












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54033396%2funity-2d-how-does-velocity-and-bouncy-physic-material-causes-the-ball-to-chang%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3
















  • The line



    rb.velocity = new Vector2(speed, speed);


    simply sets the initial velocity of the object in x and y direction.



    Since they use x = y = speed in the vector definition the ball will move diagonal in a 45° angle.



    Change those values in order to get different angle or speed.




See Physic Materials:




  • friction = 0 means that the object will not slow down. A value bigger than 0 would slow down the object over time.


  • bounciness 1 means that on collision the object won't loose any speed but instead bounce back with 100% of its velocity at the according angle. -> This is the part changing the direction of the object according to its impact angle etc



This is all handled by Unity's PhysicsEngine itself so you don't have to worry about that.






share|improve this answer


























  • Thanks for the answer! Explained me alot. But can you explain me why velocity does that? For example, a ball floating in the sky with no gravityScale and on "Start", we add a velocity of (0, -5). When we hit play, the ball will fall and touch the ground then, bounces off in the opposite direction instead of doing a rotation of 90 degrees. Can you explain me why this example, it bounces off the opposite direction whereas my question bounces and rotates 90 degrees?

    – Marc2001
    Jan 4 at 12:32













  • The difference is that if one of the two velocity axis is set to 0 it hits a flat surface with impact angle 90° .. as you surely know from natural physics and the reflection law incoming angle = outgoing angle. The same happens if you let a ball drop in nature exactly streight down => it comes back streight up. In the example however the angle was 45° as mentioned so the ball bounces of on a reflected movement vector which is -45° on the surface => the difference in that case is 90°

    – derHugo
    Jan 4 at 12:39













  • I'm terrible with physics but you explained it all. Thank you!

    – Marc2001
    Jan 4 at 22:37
















3
















  • The line



    rb.velocity = new Vector2(speed, speed);


    simply sets the initial velocity of the object in x and y direction.



    Since they use x = y = speed in the vector definition the ball will move diagonal in a 45° angle.



    Change those values in order to get different angle or speed.




See Physic Materials:




  • friction = 0 means that the object will not slow down. A value bigger than 0 would slow down the object over time.


  • bounciness 1 means that on collision the object won't loose any speed but instead bounce back with 100% of its velocity at the according angle. -> This is the part changing the direction of the object according to its impact angle etc



This is all handled by Unity's PhysicsEngine itself so you don't have to worry about that.






share|improve this answer


























  • Thanks for the answer! Explained me alot. But can you explain me why velocity does that? For example, a ball floating in the sky with no gravityScale and on "Start", we add a velocity of (0, -5). When we hit play, the ball will fall and touch the ground then, bounces off in the opposite direction instead of doing a rotation of 90 degrees. Can you explain me why this example, it bounces off the opposite direction whereas my question bounces and rotates 90 degrees?

    – Marc2001
    Jan 4 at 12:32













  • The difference is that if one of the two velocity axis is set to 0 it hits a flat surface with impact angle 90° .. as you surely know from natural physics and the reflection law incoming angle = outgoing angle. The same happens if you let a ball drop in nature exactly streight down => it comes back streight up. In the example however the angle was 45° as mentioned so the ball bounces of on a reflected movement vector which is -45° on the surface => the difference in that case is 90°

    – derHugo
    Jan 4 at 12:39













  • I'm terrible with physics but you explained it all. Thank you!

    – Marc2001
    Jan 4 at 22:37














3












3








3









  • The line



    rb.velocity = new Vector2(speed, speed);


    simply sets the initial velocity of the object in x and y direction.



    Since they use x = y = speed in the vector definition the ball will move diagonal in a 45° angle.



    Change those values in order to get different angle or speed.




See Physic Materials:




  • friction = 0 means that the object will not slow down. A value bigger than 0 would slow down the object over time.


  • bounciness 1 means that on collision the object won't loose any speed but instead bounce back with 100% of its velocity at the according angle. -> This is the part changing the direction of the object according to its impact angle etc



This is all handled by Unity's PhysicsEngine itself so you don't have to worry about that.






share|improve this answer

















  • The line



    rb.velocity = new Vector2(speed, speed);


    simply sets the initial velocity of the object in x and y direction.



    Since they use x = y = speed in the vector definition the ball will move diagonal in a 45° angle.



    Change those values in order to get different angle or speed.




See Physic Materials:




  • friction = 0 means that the object will not slow down. A value bigger than 0 would slow down the object over time.


  • bounciness 1 means that on collision the object won't loose any speed but instead bounce back with 100% of its velocity at the according angle. -> This is the part changing the direction of the object according to its impact angle etc



This is all handled by Unity's PhysicsEngine itself so you don't have to worry about that.







share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 4 at 7:58

























answered Jan 4 at 6:47









derHugoderHugo

9,87131635




9,87131635













  • Thanks for the answer! Explained me alot. But can you explain me why velocity does that? For example, a ball floating in the sky with no gravityScale and on "Start", we add a velocity of (0, -5). When we hit play, the ball will fall and touch the ground then, bounces off in the opposite direction instead of doing a rotation of 90 degrees. Can you explain me why this example, it bounces off the opposite direction whereas my question bounces and rotates 90 degrees?

    – Marc2001
    Jan 4 at 12:32













  • The difference is that if one of the two velocity axis is set to 0 it hits a flat surface with impact angle 90° .. as you surely know from natural physics and the reflection law incoming angle = outgoing angle. The same happens if you let a ball drop in nature exactly streight down => it comes back streight up. In the example however the angle was 45° as mentioned so the ball bounces of on a reflected movement vector which is -45° on the surface => the difference in that case is 90°

    – derHugo
    Jan 4 at 12:39













  • I'm terrible with physics but you explained it all. Thank you!

    – Marc2001
    Jan 4 at 22:37



















  • Thanks for the answer! Explained me alot. But can you explain me why velocity does that? For example, a ball floating in the sky with no gravityScale and on "Start", we add a velocity of (0, -5). When we hit play, the ball will fall and touch the ground then, bounces off in the opposite direction instead of doing a rotation of 90 degrees. Can you explain me why this example, it bounces off the opposite direction whereas my question bounces and rotates 90 degrees?

    – Marc2001
    Jan 4 at 12:32













  • The difference is that if one of the two velocity axis is set to 0 it hits a flat surface with impact angle 90° .. as you surely know from natural physics and the reflection law incoming angle = outgoing angle. The same happens if you let a ball drop in nature exactly streight down => it comes back streight up. In the example however the angle was 45° as mentioned so the ball bounces of on a reflected movement vector which is -45° on the surface => the difference in that case is 90°

    – derHugo
    Jan 4 at 12:39













  • I'm terrible with physics but you explained it all. Thank you!

    – Marc2001
    Jan 4 at 22:37

















Thanks for the answer! Explained me alot. But can you explain me why velocity does that? For example, a ball floating in the sky with no gravityScale and on "Start", we add a velocity of (0, -5). When we hit play, the ball will fall and touch the ground then, bounces off in the opposite direction instead of doing a rotation of 90 degrees. Can you explain me why this example, it bounces off the opposite direction whereas my question bounces and rotates 90 degrees?

– Marc2001
Jan 4 at 12:32







Thanks for the answer! Explained me alot. But can you explain me why velocity does that? For example, a ball floating in the sky with no gravityScale and on "Start", we add a velocity of (0, -5). When we hit play, the ball will fall and touch the ground then, bounces off in the opposite direction instead of doing a rotation of 90 degrees. Can you explain me why this example, it bounces off the opposite direction whereas my question bounces and rotates 90 degrees?

– Marc2001
Jan 4 at 12:32















The difference is that if one of the two velocity axis is set to 0 it hits a flat surface with impact angle 90° .. as you surely know from natural physics and the reflection law incoming angle = outgoing angle. The same happens if you let a ball drop in nature exactly streight down => it comes back streight up. In the example however the angle was 45° as mentioned so the ball bounces of on a reflected movement vector which is -45° on the surface => the difference in that case is 90°

– derHugo
Jan 4 at 12:39







The difference is that if one of the two velocity axis is set to 0 it hits a flat surface with impact angle 90° .. as you surely know from natural physics and the reflection law incoming angle = outgoing angle. The same happens if you let a ball drop in nature exactly streight down => it comes back streight up. In the example however the angle was 45° as mentioned so the ball bounces of on a reflected movement vector which is -45° on the surface => the difference in that case is 90°

– derHugo
Jan 4 at 12:39















I'm terrible with physics but you explained it all. Thank you!

– Marc2001
Jan 4 at 22:37





I'm terrible with physics but you explained it all. Thank you!

– Marc2001
Jan 4 at 22:37




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54033396%2funity-2d-how-does-velocity-and-bouncy-physic-material-causes-the-ball-to-chang%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas