• 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 Hims · Jun 29, 2012 at 12:18 PM · spawnfootball

reset the ball in football game..

i am working on a fooball game and i am trying to write the script to re spawn the ball after player kicks the ball.. following is the script i wrote but its not working...can any one help me to find the mistake in this script.. the ball is applied rigidbody.

using UnityEngine;

using System.Collections;

public class BallSpawnTest : MonoBehaviour {

private Vector3 initialPos; // Hold the ball's initial position, and reset it's position to this after every kick

 private const float        KICK_SEQUENCE_TIME = 3.0f;        // Time from kick start until the game is reset
     // Timer to make sure we only use the first collision detected
 
 // Called when the game starts
 void Start()
 {
     // Remeber the ball's initial position. Later we will use it to restore the ball after every kick
     initialPos = transform.position;
 }
 
 
 
 
 

private IEnumerator RestartBall() {

// Wait a few seconds for the kick to end and the wall to come crashing down before resetting the game

     yield return new WaitForSeconds(KICK_SEQUENCE_TIME);


     Debug.Log("function called");


     print("this function was called");


     // Reposition the ball at it's initial place


     transform.position = initialPos;
     


     // Cancel all previous forces to keep the ball still


     rigidbody.velocity = Vector3.zero;

     rigidbody.angularVelocity = Vector3.zero;
     
 }

}

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by mviuk · Jun 29, 2012 at 12:52 PM

You're not actually starting the RestartBall coroutine.

You need to add StartCoroutine(RestartBall()); to your code somewhere. If you are going to have a Kick function, this would be a great place to fire off the coroutine.

Something like:

 public void OnClick()
 {
     rigidbody.AddForce(new Vector3(1000,300,0));
     StopAllCoroutines();
     StartCoroutine(RestartBall());
 }
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 Hims · Jun 29, 2012 at 02:18 PM 0
Share

my modified script is as following the RestartBall() is not working the ball keeps moving slowly after the collision and it is not re positioning...i dont know what is going wrong..help..me.. the RestartBall() when called it prints the initial position in the print msg..but after that i wrote "gameObject.transform.position = initialPos;" this line is used to bring the ball to the starting position but ball is not being s$$anonymous$$dy and it is not re positioning..

using UnityEngine; using System.Collections;

public class BallSpawnTest : $$anonymous$$onoBehaviour {

 private Vector3    initialPos;                        // Hold the ball's initial position, and reset it's position to this after every kick
 

// private const float $$anonymous$$IC$$anonymous$$_SEQUENCE_TI$$anonymous$$E = 3.0f; // Time from kick start until the game is reset //

 // Called when the game starts
 void Start()
 {
     // Remeber the ball's initial position. Later we will use it to restore the ball after every kick
     initialPos = transform.position;
 }
 
 void Update ()
 {
     Debug.Log("update function called");
     print(initialPos);
     RestartBall();
 }
 /*  this did not work. any suggestions..?
  void OnTriggerEnter(Collider other) {
 
      if(other.gameObject.name=="ball")
      {
          StopAllCoroutines();
         StartCoroutine(RestartBall());
     }
      }

/ / this did not work. any suggestions..? public void OnClick() { rigidbody.AddForce(new Vector3(1000,300,0)); StopAllCoroutines(); StartCoroutine(RestartBall()); }*/

public IEnumerator RestartBall() {

     // Wait a few seconds for the kick to end and the wall to come crashing down before resetting the game
 
     
     
     yield return new WaitForSeconds(5);
      
     Debug.Log("function called");
     
     print("this function was called");
     // Reposition the ball at it's initial place
     
     
     
     gameObject.transform.position = initialPos; //not working
     
     print(initialPos); //working
     
     // Cancel all previous forces to keep the ball still
         rigidbody.velocity = Vector3.zero;
     rigidbody.angularVelocity = Vector3.zero;
 }

     

 
 }
 


 
 


 


 
 


 

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

7 People are following this question.

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

Related Questions

Spawn referencable objects with RayCast 1 Answer

Player Fast Travels 1 Answer

Room generator spawn 1 unique room 1 Answer

Script needs fixing ?! 1 Answer

Why not can't spawn the 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