Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Ball Movement Issue

Discussion in 'Scripting' started by kasana_94, Jul 30, 2015.

  1. kasana_94

    kasana_94

    Joined:
    Apr 13, 2014
    Posts:
    24
    Hello everyone I am having a little issue with this simple project
    I am making a 3d breakout game in which I want to make the ball move in X and Z direction respectively. I have used the code for ball movement from Unity's Breakout game tutorial.

    After using the code, the ball is only moving horizontally (X DIRECTION) at the same spot.

    How do I make it move in z direction also freely

    Thanks in advance

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Ball : MonoBehaviour {
    5.  
    6.     public float ballInitialVelocity= 600f;
    7.     private Rigidbody rb;
    8.     private bool ballInPlay;
    9.  
    10.     // Use this for initialization
    11.     void Awake () {
    12.    
    13.         rb = GetComponent<Rigidbody> ();
    14.     }
    15.    
    16.     // Update is called once per frame
    17.     void Update () {
    18.    
    19.         if(Input.GetButtonDown("Fire1")&& ballInPlay == false)
    20.         {
    21.             transform.parent = null;
    22.             ballInPlay = true;
    23.             rb.isKinematic = false;
    24.             rb.AddForce(new Vector3(ballInitialVelocity, ballInitialVelocity,0));
    25.  
    26.         }
    27.  
    28.  
    29.     }
    30. }
    31.  
     

    Attached Files:

  2. martinmr

    martinmr

    Joined:
    Mar 25, 2015
    Posts:
    325
    if you want x z you should do

    Code (CSharp):
    1. rb.AddForce(new Vector3(ballInitialVelocity, 0, ballInitialVelocity));
    don't know what the rest of your scripts do, but that's the only point you adding force to your ball
     
  3. kasana_94

    kasana_94

    Joined:
    Apr 13, 2014
    Posts:
    24
    Thanks for the reply.. It really worked.. but I am facing an issue, whenever I play the game the ball slows down after each impact, and finally stop in between.
    I guess the initial velocity of 600 zeros down that's why the ball stops

    please help me with this.


    thanks in advance
     
  4. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    Sounds like gravity and friction are working exactly as they should. Initial velocity and minimum velocity are two different concepts: are you wanting a minimum velocity?

    You may also want to look into OnCollisionEnter and basically "re-impact" the ball each time it hits the paddle to add more force to it. I would also look into making gravity pull in the direction of the paddle instead, so that a minimum velocity won't ever be necessary (it'll just "fall down" toward the paddle if it loses momentum and allow you to re-impact it).
     
    Last edited: Jul 31, 2015