Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 defenceplox · Jul 02, 2013 at 03:52 PM · velocityspeedrespawnbreakout

Simple breakout game issue

so ive been working on a simple Breakout style game. problem is, when you lose the ball below the paddle, the ball wont reset its speed to minimum. heres the script for the ball.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class Ball : MonoBehaviour {
     public float multiplier = 1.1f;
     public float minSpeed = 5;
     public float maxSpeed = 10;
     
     public float curSpeed = 0;
     
     private Transform _t;
     
     public List<GameObject> blocks;
     
     void Awake() {
         _t = transform;
         
         blocks = new List<GameObject>();
     }    
     // Use this for initialization
     void Start() {
         blocks.AddRange( GameObject.FindGameObjectsWithTag( "Block" ) );
         
         ResetBall();    
     }
     
     // Update is called once per frame
     void Update() {
         if( blocks.Count <1 )
             WonLevel();
         if( _t.position.y < -5 )
             MissedBall ();
     }    
     void FixedUpdate() {
         curSpeed = Vector3.Magnitude( rigidbody.velocity );
         
         if (curSpeed > maxSpeed ) 
             rigidbody.velocity /= curSpeed / maxSpeed;
         
         if ( curSpeed < minSpeed && curSpeed > 0 )
             rigidbody.velocity /= curSpeed / minSpeed;
     }
     
     void OnCollisionEnter( Collision col ) {    
         rigidbody.velocity += rigidbody.velocity * multiplier;
         
         if( col.gameObject.CompareTag( "Block" ) )
             blocks.Remove( col.gameObject );        
     }
     
     private void MissedBall() {
         if( Lives.curLives-- < 2 )
             GameOver();
         else 
             ResetBall();
             
         
         
     }    
     
     private void ResetBall() {
         _t.position = new Vector3( Random.Range( -Player.xBoundry + 2 , Player.xBoundry - 2), 2, Player.zPosition );
         _t.LookAt( GameObject.FindGameObjectWithTag( "Player" ).transform.position );
         
                 rigidbody.AddRelativeForce( new Vector3( 0, 0, minSpeed ) );
     }
     
     private void GameOver() {
         Debug.Log(" Game Over");
         _t.renderer.enabled = false;
         rigidbody.velocity = Vector3.zero;
         _t.position = Vector3.zero;
     }    
     
     private void WonLevel() {
         Debug.Log( "We Won Level" );    
     }    
 }
 
Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by InfiniBuzz · Jul 02, 2013 at 03:59 PM

As the name says AddRelativeForce adds the given force to the rigidbody regardless of what force / velocity is already applied. You probably want to use

 private void ResetBall() {
 
    _t.position = new Vector3( Random.Range( -Player.xBoundry + 2 , Player.xBoundry - 2), 2, Player.zPosition );
    _t.LookAt( GameObject.FindGameObjectWithTag( "Player" ).transform.position );
 
    rigidbody.velocity = Vector3.zero; // Add this line
    rigidbody.AddRelativeForce( new Vector3( 0, 0, minSpeed ) );
 }

hope it solves your problem

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 defenceplox · Jul 02, 2013 at 04:19 PM 0
Share

thanks so much

avatar image
0

Answer by robertbu · Jul 02, 2013 at 03:57 PM

In ResetBall() you are adding a relative force, but you are not canceling any velocity already on the ball. Try adding this at line 65:

 rigidbody.velocity = Vector3.zero;

Or you can assign the velocity directly:

 rigidbody.velocity = transform.foward * minSpeed;

Note that minSpeed is independent of mass, so it will need to be substantially smaller if you have the default mass of 1.0.

Comment
Add comment · 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

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.

Follow this Question

Answers Answers and Comments

16 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

Related Questions

Mass affecting velocity 2 Answers

How to calculate swipe speed on iOS 2 Answers

Is unity magnitude in km/h or m/s? 1 Answer

Advice controling speed of a rigidbody 1 Answer

How do I accelerate particles in Shuriken? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges