- Home /
problem with ball mechanics
Hey guys im trying to make a "block breaker" game and i want to my ball to bounce like in the game and i managed to do this script
void Start ()
{
rigidbody.AddForce (150f, 450f, 0);
}
// Update is called once per frame
void Update () {
}
but in the best case its only bounce back to where the ball was dropped from and it aint going sideways as it should do thanks
it seems as the ball hit the paddle its loses momentum also i changed the bounce threshold to 0 on project setting and bounce combine to max on phyball(which is the material for the ball)
now its going sideways as it should do but up and down only the where the ball was dropped at the best(also it seems the ball loses momentum if its going sideways)
Answer by guitarxe · Dec 09, 2013 at 04:32 PM
Unity's physics mimics real life. There is nothing in real life that would make a ball that is dropped from a height onto a uniform surface to bounce to the left or right. It will bounce straight back up.
In order to make the ball bounce to the left or right the way you want to, you will have to write additional code to handle that behaviour. Start first by simply making it bounce in one particular direction all the time. Once you get that working, you can think about getting it to bounce in different directions depending on where the ball lands on the paddle.
oky i rewrote both my paddle and my ball scripts and now it go sideways as it should do but not up(till the edge of the screen as it should do) ball script: **using UnityEngine; using System.Collections;
public class BallScript : $$anonymous$$onoBehaviour {
// Use this for initialization
void Start ()
{
rigidbody.AddForce (0, 450f, 0);
}
// Update is called once per frame
void Update () {
}
}**
paddle script:
**using UnityEngine;
using System.Collections;
public class PadleScript : $$anonymous$$onoBehaviour {
public float peddlespeed=10f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update ()
{
transform.Translate (peddlespeed * Time.deltaTime * Input.GetAxis ("Horizontal"), 0, 0);
}
void OnCollisionEnter (Collision col)
{
foreach (ContactPoint Contact in col.contacts)
{
if ( Contact.thisCollider==collider)
{
float english = Contact.point.x - transform.position.x;
Contact.otherCollider.rigidbody.AddForce(300f*english,0,0);
}
}
}
}**
Not all the way up to the top edge of the screen? You're adding a certain amount of force on the ball, but you know that there is gravity also acting on the ball which makes it fall back down, right? You can change this gravity settings from project settings > physics, or on the RigidBody
component of your ball, just uncheck the Use Gravity
box.
Since you are trying to recreate a "breakout" game, once a ball is launched there should be no other forces acting on it, such as friction, otherwise the ball will eventually lose its velocity and come to a stop - not what you want in a "breakout" game.
Your answer
Follow this Question
Related Questions
Why is my object tilting when moving? 2 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Rigidbody AI script needed 0 Answers
add force get stronger over time slow. 2 Answers
Flight sim script help 0 Answers