- Home /
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;
}
}
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());
}
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
Follow this Question
Related Questions
Spawned Object spawning to low. 2 Answers
How to make a game with never ending waves of zombies like in COD zombies? 0 Answers
Zombie Wave Spawning script 1 Answer
Spawn enemy at an offset 2 Answers
Snap Objects when Spawning 0 Answers