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 andriii25 · Jun 23, 2014 at 12:27 PM · velocityballdisappearinvokepong

Ball disappears after invoking a method

Hi! I'm trying to make a Pong game as a "challenge", it pretty much works, except I'm having problems when a player scores a point.

The following should happen:

  • Game starts, ball in 0,0 with zero velocity

  • Wait 3 seconds

  • Invoking the SetVelocity method, which sets a starting velocity for the ball

  • Ball bouncing of the top and bottom walls, and the paddles

  • A player scores a point

  • Calls for the Start() method so:

  • Set the ball's velocity to 0

  • Set the ball's position to 0,0

  • Wait 3 seconds

  • Invoking the SetVelocity method, which sets...

And this goes on..

But, when after the ball position is set after when a player scores a point, the ball gets to center, it stays there for a little but definitely a lot less than 3 seconds and disappears. And I have no idea why. Any suggestions?

Oh and by the way here's the ball's code: (also tried if not calling the start method, but doing the same, no difference)

 using UnityEngine;
 using System.Collections;
 
 public class Ball : MonoBehaviour
 {
 
     public float speed;
     public ScoreHandler scorehandler;
     public float delay = 3.0f;
 
     void Start ()
     {
         rigidbody2D.velocity = new Vector2 (0f, 0f);
         //Debug.Log ("Setted velocity to 0");
         transform.position = new Vector3 (0.0f, 0.0f, 0.0f);
         //Debug.Log ("Setted position to the middle");
         Invoke ("SetVelocity", delay);
         //Debug.Log ("Invoked SetVelocityMethod");
     }
 
     void Update(){
         //Debug.Log (rigidbody2D.velocity);
     }
 
     void SetVelocity (){
         //Debug.Log ("Setting starting velocity");
         rigidbody2D.velocity = Vector2.one.normalized * speed;
         //Debug.Log ("Setted starting velocity");
     }
 
 
     float hitFactor (Vector2 ballPos, Vector2 racketPos,
                     float racketHeight)
     {
                 
         return (ballPos.y - racketPos.y) / racketHeight;
     }
 
     void OnCollisionEnter2D (Collision2D col)
     {
         //Debug.Log (col.gameObject.name);
         if (col.gameObject.name == "paddleLeft")
         {
             //Debug.Log (col.gameObject.name);
             float y = hitFactor (transform.position,
                               col.transform.position,
                               ((BoxCollider2D)col.collider).size.y);
             
             Vector2 dir = new Vector2 (1, y).normalized;
             
             rigidbody2D.velocity = dir * speed;
         } else if (col.gameObject.name == "paddleRight")
         {
             float y = hitFactor (transform.position,
                               col.transform.position,
                               ((BoxCollider2D)col.collider).size.y);
             
             Vector2 dir = new Vector2 (-1, y).normalized;
             
             rigidbody2D.velocity = dir * speed;
         } else if (col.gameObject.name == "wallRight")
         {
             scorehandler.AddLeft ();
             /*rigidbody2D.velocity = new Vector2(0f,0f);
             transform.position = new Vector3 (0.0f, 0.0f);
             Invoke ("SetVelocity", delay);*/
             Start();
 
 
         } else if (col.gameObject.name == "wallLeft")
         {
             scorehandler.AddRight ();
             /*rigidbody2D.velocity = new Vector2(0f,0f);
             transform.position = new Vector3 (0.0f, 0.0f);
             Invoke ("SetVelocity", delay);*/
             Start();
 
         }
     }
 }
 



Comment
Add comment · Show 3
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 Nerevar · Jun 23, 2014 at 12:56 PM 0
Share

You should not Call Start() in different parts of your code, create another function for example Init() and call it in Start() and wherever you want to reset your parameters. I don't know if that's the source of your problem but it would be safer to do like this :p .

avatar image andriii25 · Jun 23, 2014 at 01:48 PM 0
Share

Yeah, I thought it may be a problem, so I basicly copied exactly what the Start() does, but as I said, no difference, but thanks it was still helpful. :D

avatar image andriii25 · Jun 24, 2014 at 01:07 PM 0
Share

Problem solved, actually the Ball was destroyed by an auto-destroy system which was the TrailRenderer!

0 Replies

· Add your reply
  • Sort: 

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

2 People are following this question.

avatar image avatar image

Related Questions

Ball stuck horizontally between rackets in pong 0 Answers

Prevent ball from going straight on x and y axis 2 Answers

What is the best way to move a paddle accurately in a circle? 2 Answers

A ball drops down, how to reverse the velocity? 2 Answers

Why my ball is slowing after contact with other object? 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