• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by mrZimaus · Jun 10, 2014 at 03:58 PM · ballnewpong

Ball wont go faster

Hello unity newbie here. I have a problem with one of my scripts. I´m trying to make a simple pong game where the ball will go faster everytime it hits one of the players. But the ball only goes slower! Here is my script:

 using UnityEngine;
 using System.Collections;
 
 public class Ball : MonoBehaviour {
     public int ballSpeed;
     
     void Start () {
         rigidbody.AddForce(new Vector3(Random.Range(-100.0F, 100.0F), 0, 0) * ballSpeed * Time.deltaTime);
     }
     
     void OnCollisionEnter (Collision col)
     {
         if(col.gameObject.name == "Player01")
         {
             float X = Random.Range(-5.0F, -15.0F);
             float Y = Random.Range(6.0F, -6.0F);
             ballSpeed += 100;
             rigidbody.AddForce(new Vector3(X, Y, 0) * ballSpeed * Time.deltaTime);
         }
         else if(col.gameObject.name == "Player02")
         {
             float X = Random.Range(5.0F, 15.0F);
             float Y = Random.Range(6.0F, -6.0F);
             ballSpeed += 100;
             rigidbody.AddForce(new Vector3(X, Y, 0) * ballSpeed * Time.deltaTime);
         }
     }
 }
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JayOhhh · Jun 10, 2014 at 06:13 PM 0
Share

First thing I notice is that you aren't using the 2D physics components. Are your GameObjects in your scene setup with 2D colliders?

avatar image mrZimaus · Jun 10, 2014 at 07:10 PM 0
Share

I´m not using 2d colliders

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by tanoshimi · Jun 10, 2014 at 06:25 PM

Two comments:

  • Firstly, I'd make ballSpeed a float. (i.e. public float ballSpeed; and ballSpeed += 100f;)

  • I don't know why you're multiplying the force by Time.deltaTime, but I'd be almost certain that you don't want to, so try removing that from the force assignment. Time.deltaTime is generally used for changing values in a frame-independent manner, but it's not applicable here since AddForce is an instantaneous event. When you do remove this term, you'll probably need to adjust the scale of the other values, since you're currently multiplying the force by an arbitrary small number that's probably around 0.02.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mrZimaus · Jun 10, 2014 at 07:19 PM 0
Share

Didnt work just started spasming out sometimes when colliding

avatar image tanoshimi · Jun 10, 2014 at 07:54 PM 0
Share

What does "didn't work" mean? Didn't do anything? Gave an error message? Didn't speed up? You need to help us help you...

avatar image
0

Answer by MissionCalice8 · Jun 10, 2014 at 06:55 PM

Do you know about colliders and triggers? Try putting a a collider on the paddles and the ball, then check "is trigger". I'm not sure if this will work. You'll have to add a tag to each of the paddles.

using UnityEngine; using System.Collections; public class Collider : MonoBehaviour { void OnTriggerEnter(Collider other) { if (other gameObject.tag == "Paddle") { rigidbody.AddForce(10, 10, 0); } } }

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mrZimaus · Jun 10, 2014 at 07:10 PM 0
Share

Did not work, seems like it should tough

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Method keeps looping when using waitForSeconds 1 Answer

Need help with begginner script! 1 Answer

function Start () problem!! 3 Answers

I need help !!! -3 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges