- Home /
Question by
maxnamd · Sep 30, 2017 at 04:25 PM ·
c#unity 5instantiate
Generate a new ball when the First Ball is release.
Hello I'm making a game that is similar to a FB Messenger Basketball game. The first ball is working but when the 2nd ball is generated... the 2nd ball is not working unlike the first ball..
public GameObject ball; //player reference to ball
public GameObject playerCamera; //player reference to first person controller's camera
public GameObject newParent; //
public float ballDistance = 2.25f; //ball distance from player
public float ballThrowForce = 5f; //ball throwing force
public float resetTimer = 2f; //Generate a ball timer.
private bool isHoldingBall = true;//Determine if the player is holding a ball
private bool isGenerated = false;//Determine if the ball is generated
// Use this for initialization
void Start () {
ball.GetComponent<Rigidbody> ().useGravity = false;
}
// Update is called once per frame
void Update () {
if (isHoldingBall == true){
ball.transform.position = playerCamera.transform.position + playerCamera.transform.forward * ballDistance;
if (Input.GetMouseButtonDown (0)) {
ball.GetComponent<Rigidbody> ().useGravity = true;
ball.GetComponent<Rigidbody> ().AddForce (playerCamera.transform.forward * ballThrowForce);
isHoldingBall = false;
}
}
if (isHoldingBall == false) {
resetTimer -= Time.deltaTime;
if(resetTimer <= 0){
if(isGenerated == false){
//Something is wrong here
GameObject ballInstance = (GameObject)Instantiate (ball, playerCamera.transform.forward * ballDistance, transform.rotation);
ballInstance.transform.parent = newParent.transform;
isHoldingBall = true;
isGenerated = true;
}
resetTimer = 2f;
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Create random order and go through till the end (C#) 1 Answer
Is there anyway to delete clones that are local variables? 1 Answer
How to make TCG Deck (Instatiate based on Prefab) 1 Answer
How to instantiate platforms like Doodle Jump game ? 1 Answer
How can I Instantiate two new GameObjects at the same time? (Attempting Asteroids style game) 2 Answers