- Home /
Move ball at constant speed on screen
I want to move ball constantly on screen as per my question title suggest. If your suggestion is using physics based movement then it is advantage for me.
Basically I am working on following game : Arkanoid
If you suggest me any link or tutorial for this then also it is okay, I will go over through that also. Please give your suggestion.
Answer by lordlycastle · Apr 21, 2015 at 02:02 PM
Ah, a Breakout game, I created one too. There are many way to deal with movement e.g. rigidbody.AddForce
, or rigidbody.velocity = …
; and change you make to rigidbody are physics based to the engine will check if movement is possible before actually doing it. Here is a link to Unity’s breakout game tutorial, and I’m sure a Google search will give you many.
Below is a Ball class I used. I think my game was top-down, so you should change the axis accordingly.
public class Ball : MonoBehaviour
{
public float MinimumSpeed = 25;
public float MaximumSpeed = 30;
//To prevent the ball from keep bouncing horizontally we enforce a minimum vertical movement
public float MinimumVerticalMovement = 0.1F;
//Don't move the ball unless you tell it to
private bool hasBeenLaunched = false;
void Update()
{
if (hasBeenLaunched)
{
//Get current speed and direction
Vector3 direction = rigidbody.velocity;
float speed = direction.magnitude;
direction.Normalize();
//Make sure the ball never goes straight horizotal else it could never come down to the paddle.
if (direction.z > -MinimumVerticalMovement && direction.z < MinimumVerticalMovement)
{
//Adjust the y, make sure it keeps going into the direction it was going (up or down)
direction.z = direction.z < 0 ? -MinimumVerticalMovement : MinimumVerticalMovement;
//Adjust the x also as x + y = 1
direction.x = direction.x < 0 ? -1 + MinimumVerticalMovement : 1 - MinimumVerticalMovement;
//Apply it back to the ball
rigidbody.velocity = direction * speed;
}
if (speed < MinimumSpeed || speed > MaximumSpeed)
{
//Limit the speed so it always above min en below max
speed = Mathf.Clamp(speed, MinimumSpeed, MaximumSpeed);
//Apply the limit
//Note that we don't use * Time.deltaTime here since we set the velocity once, not every frame.
rigidbody.velocity = direction * speed;
}
}
}
//When the bottom of the field it hit destroy the ball.
private void OnTriggerEnter(Collider other)
{
if (other.name == "Bottom")
{
Destroy(this.gameObject);
}
}
public void Launch()
{
//Create a random vector but make sure it always point "up" (z axis in this case) else it could be launched straight down
Vector3 randomDirection = new Vector3(Random.Range(-1.0F, 1.0F), 0, Mathf.Abs(Random.value));
//Make sure we start at the minimum speed limit
randomDirection = randomDirection.normalized * MinimumSpeed;
//Apply it to the rigidbody so it keeps moving into that direction, untill it hits a block or wall
rigidbody.velocity = randomDirection;
hasBeenLaunched = true;
}
}
@lordlycastle, Thanks for your reply. Straight pasting of your code generate just horizontal ball movement in game. One more thing, I want only x and y movement of ball.
Please give some suggestion in this.
@siddharth3322 Like I said, you’ll have to adjust the axis, as I made this top-down, and that is probably not you are doing. I did that by mistake, and never fixed it because I was just doing the project to learn. Don’t straight post the code, understand what’s happening and then apply to your project, yourself. The comments are pretty self-explanatory, it doesn’t just generate horizontal movement, that probably because you are viewing with x-y plane, while this makes the ball move in x-z.
Answer by user6116 · Jan 26, 2018 at 06:34 PM
@lordlycastle After an hour of searching, your code got me to the answer i was looking for, thanks!
Your answer
Follow this Question
Related Questions
Unity 2d RigidBody Movement stutter 0 Answers
Hello everyone, I would like to create a Geometry dash like triangle Transporter in unity. 0 Answers
The game object penetrates the collision body when moving too fast 1 Answer
How do i make a cube move (Continuosly without stopping) when i press a button once in unity 2D 2 Answers
I want my vehicle to keep moving left/right while the buttons are held down? 1 Answer