putting the audio.play(); in start worked find but its not working when I place it after void OnCollisionEnter2d (Collision2d collision)
so I must conclude I did something to prevent it detecting the collision?? My code looks pretty much like the example
any ideas??
using UnityEngine;
using System.Collections;
public class Ball : MonoBehaviour {
private Paddle paddle;
private Vector3 paddleToBallVector;
private bool hasStarted= false;// has the game started?
private Vector2 addedForce = new Vector2 (0.5f, 0.0f);
// how much sideways force to add to prevent stalling loop of ball with zero change in x
void Start () {
paddle = GameObject.FindObjectOfType<Paddle>();
paddleToBallVector = this.transform.position - paddle.transform.position; //the offset to paddle
print (paddleToBallVector.y);
// audio.Play ();
}
void Update () {
if (!hasStarted){// do while game not started
this.transform.position = paddle.transform.position + paddleToBallVector;//locks ball relative to paddle
if (Input.GetMouseButtonDown (0)){
print ("left mouse button clicked so Launch Ball!");
this.rigidbody2D.velocity = new Vector2 (2f, 10f);//shoot ball up
// this.rigidbody2D.AddForce(addedForce, ForceMode2D.Force);// oops trapsball on right
this.rigidbody2D.AddTorque(0.6f,ForceMode2D.Impulse);
hasStarted = true;//game has started
}
}
//else {//do each fram while game running
//if(this.rigidbody2D.velocity.x==0){
// this.rigidbody2D.AddForce(addedForce, ForceMode2D.Force);
// add lateral push every frame if started and x velocity ==0
//}
//this.rigidbody2D.velocity = this.rigidbody2D.velocity + paddle.rigidbody2D.velocity + paddle.rigidbody2D.velocity;
//}
}
void OnCollisionEnter2d (Collision2D collision){
//if (hasStarted){
audio.Play();
//}
}
}
