roll-a-ball Counter issue?
Hello,
I tried to modify roll-a-ball tutorial a little, I have 2 pick ups objects tagged "green" , "red", and "skull" tag for errors and a "flag" taged object for the player to end the game and check for score. the code is attached to my player object .
I have 3 major issues:
the counter sometimes shows wrong values? in runtime when collecting an object it decrement the value instead of incrementing"sometimes"?
the flag conditions are ignored and the win panel shows whenever the player collides with the flag object, regardless of the if statment? I'm not sure how the if statements block is entered .
the stop_player function is ignored, as the player is still able to more :/
my code is the following:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class PlayerController : MonoBehaviour
{
public float PlayerSpeed = 4f;
//text to show counters in UI
public Text countText_green;
public Text countText_red;
//the score text
public Text winText;
//the fixed required random numbers in UI
public Text x_red;
public Text y_green;
public GameObject Win_label;
public Text errorText;
private Rigidbody rb;
private int count_red;
private int count_green;
private int count_error;
private int reds;
private int greens;
void Start ()
{
reds = Random.Range (1, 5);// creates a number between 1 and 12
greens = Random.Range (1, 5);// creates a number between 1 and 12
rb = GetComponent<Rigidbody> ();
count_green = 0;
count_error = 0;
SetCountText ();
winText.text = "";
errorText.text = "";
x_red.text = "" + reds;
y_green.text = "" + greens;
}
void FixedUpdate ()
{
/*float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rb.AddForce (movement * speed );*/
if (Input.GetKey (KeyCode.UpArrow)) {
//rb.AddForce(Vector3.forward * PlayerSpeed * Time.deltaTime);
rb.transform.Translate (Vector3.forward * PlayerSpeed * Time.deltaTime);
}
if (Input.GetKey (KeyCode.DownArrow)) {
rb.transform.Translate (
Vector3.back * PlayerSpeed * Time.deltaTime);
}
if (Input.GetKey (KeyCode.RightArrow)) {
rb.transform.Rotate (
new Vector3 (0, 100f, 0) * Time.deltaTime);
}
if (Input.GetKey (KeyCode.LeftArrow)) {
rb.transform.Rotate (
new Vector3 (0, -100f, 0) * Time.deltaTime);
}
}
void OnTriggerEnter (Collider other)
{
if (other.gameObject.CompareTag ("Green")) {
other.gameObject.SetActive (false);
count_green = count_green + 1;
countText_green.text = count_green.ToString ();
}
if (other.gameObject.CompareTag ("Red")) {
other.gameObject.SetActive (false);
count_red = count_red + 1;
countText_red.text = count_red.ToString ();
}
if (other.gameObject.CompareTag ("skull")) {
other.gameObject.SetActive (false);
count_error = count_error + 1;
}
if (other.gameObject.CompareTag ("flag")) {
//if player still didn't reach required numbers
if (count_red < reds || count_green < greens) {
//continue playing
}
//wining cases
else if (count_red == reds
&& count_green == greens) {
Stop_player ();
Win_label.gameObject.SetActive (true);
winText.gameObject.SetActive (true);
errorText.text = "" + count_error.ToString ();
//total score is 10 for each error decrement 1
int score = 10 - (count_error);
winText.text = score.ToString ();
}
//if player collected more than required
else if (count_red >= reds || count_green >= greens) {
Stop_player ();
int extra_red = 0;
int extra_green = 0;
if (count_red > reds)
extra_red = count_red - reds;
if (count_green > greens)
extra_green = count_green - greens;
//add wrong numbers to count errors to decrement score
count_error = count_error + extra_red + extra_green;
Win_label.gameObject.SetActive (true);
winText.gameObject.SetActive (true);
errorText.text = "" + count_error.ToString ();
//total score is 10 for each error decrement 1
double score_D = 20 - count_error;
string score_S = score_D.ToString ();
int score = int.Parse (score_S);
winText.text = score.ToString ();
}
//}//end wining case
}
}
void Stop_player ()
{
Destroy (rb);
PlayerSpeed = 0f;
}
void SetCountText ()
{
countText_green.text = count_green.ToString ();
countText_red.text = count_red.ToString ();
}
}
Any help would be grateful. thank you <3
Your answer
Follow this Question
Related Questions
Am I misunderstanding how this code should function? 1 Answer
mathf.abs(0) is higher than 90, why? 1 Answer
Can't Locate gameobject in array 3 Answers
problem with lists 0 Answers