- Home /
this script is to reset the ball to its initial position after every kick in foorball game..
The problem with this script is that the 'OnCollisionEnter' function is not being called when the ball collide with the collider..can any one help me to find whats the wrong in this script..?? using UnityEngine; using System.Collections;
public class BallSpawnTest : MonoBehaviour
{
private Vector3 initialPos;
private Quaternion originalRotation;
// 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;
originalRotation = transform.rotation;
}
void Update ()
{
Debug.Log("update function called");
// Reset();
}
void OnCollisionEnter(Collider collide) {
if(collide.gameObject.name=="Sphere")
{
Debug.Log("collied");
Reset();
}
}
private IEnumerator RestartBall()
{
yield return new WaitForSeconds(5);
Debug.Log("in restart bal function");
transform.position = initialPos;
transform.rotation = originalRotation;
}
public void Reset()
{
StartCoroutine(RestartBall());
rigidbody.velocity = Vector3.zero;
rigidbody.angularVelocity = Vector3.zero;
Debug.Log("function called");
}
}
Comment
Answer by Wolin · Jul 03, 2012 at 01:48 PM
Check if the both objects, a ball and a collider, have rigidbodies.
Your answer
Follow this Question
Related Questions
Destroy Turret with machine Gun 0 Answers
Collision problem 0 Answers
Making the object collide while moving in the forward dir. (Vector3.forward) 1 Answer
OnCollisionEnter function problem. JS 2 Answers
help sound on collision when key down 2 Answers