I m working on an endless runner 3D project,
Now I can collect coins and coins are written on left corner up
I want to add a script that counts the coins and writes as a highscore when the player is dead to the gameoverMenu, under GAME OVER here:
This is my coin script :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Coin : MonoBehaviour
{
void Start()
{
}
// Update is called once per frame
void Update()
{
transform.Rotate(100 * Time.deltaTime, 0, 0);
}
private void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
FindObjectOfType<AudioManager>().PlaySound("CoinSound");
PlayerManager.numberOfCoins += 1;
//Debug.Log("Coins" + PlayerManager.numberOfCoins);
Destroy(gameObject);
}
}
}
AND THIS IS PLAYERMANAGER script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayerManager : MonoBehaviour
{
public static bool gameOver;
public GameObject gameoverPanel;
public static bool isGameStarted;
public GameObject startingText;
public static int numberOfCoins;
public Text coinsText;
public Text highscoreText;
void Start()
{
gameOver = false;
Time.timeScale = 1;
isGameStarted = false;
numberOfCoins = 0;
}
private void Awake()
{
}
void Update()
{
if (gameOver)
{
Time.timeScale = 0;
gameoverPanel.SetActive(true);
}
coinsText.text = "Coins:" + numberOfCoins;
if (SwipeManager.tap)
{
isGameStarted = true;
Destroy(startingText);
}
}
}
I THINK I HAVE TO PUT THE COIN MANAGEMENT THERE I TRIED WITH SOME TUTORS BUT DIDNT HELP
here also PLAYERCONTROLLER script :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
private CharacterController controller;
private Vector3 direction;
public float forwardSpeed;
public float maxSpeed;
private int desiredLane = 1; //0 left 1 middle 2 right
public float laneDistance = 4; // distance between two lanes
public float JumpForce;
public float Gravity = -20;
public Animator animator;
private bool isSliding = false;
void Start()
{
controller = GetComponent<CharacterController>();
}
void Update()
{
if (!PlayerManager.isGameStarted)
return;
if(forwardSpeed < maxSpeed)
forwardSpeed += 0.1f * Time.deltaTime;
animator.SetBool("isGameStarted", true);
direction.z = forwardSpeed;
animator.SetBool("isGrounded", controller.isGrounded);
if (controller.isGrounded)
{
direction.y = -1;
if (SwipeManager.swipeUp)
{
Jump();
}
}
else
direction.y += Gravity * Time.deltaTime;
if (SwipeManager.swipeDown && !isSliding)
{
StartCoroutine(Slide());
}
if (SwipeManager.swipeRight)
{
desiredLane++;
if (desiredLane == 3)
desiredLane = 2;
}
if (SwipeManager.swipeLeft)
{
desiredLane--;
if (desiredLane == -1)
desiredLane = 0;
}
Vector3 targetPosition = transform.position.z * transform.forward + transform.position.y * transform.up;
if (desiredLane == 0)
{
targetPosition += Vector3.left * laneDistance;
}
else if (desiredLane == 2)
{
targetPosition += Vector3.right * laneDistance;
}
// transform.position = Vector3.Lerp(transform.position, targetPosition, 80 * Time.deltaTime);
// controller.center = controller.center;
if (transform.position == targetPosition)
return;
Vector3 diff = targetPosition - transform.position;
Vector3 moveDir = diff.normalized * 25 * Time.deltaTime;
if (moveDir.sqrMagnitude < diff.sqrMagnitude)
controller.Move(moveDir);
else
controller.Move(diff);
}
private void FixedUpdate()
{
if (!PlayerManager.isGameStarted)
return;
controller.Move(direction * Time.fixedDeltaTime);
}
private void Jump()
{
direction.y = JumpForce;
}
private void OnControllerColliderHit(ControllerColliderHit hit)
{
if (hit.transform.tag == "Obstacle")
{
PlayerManager.gameOver = true;
FindObjectOfType<AudioManager>().PlaySound("GameOverSound");
}
}
private IEnumerator Slide()
{
isSliding = true;
animator.SetBool("isSliding", true);
controller.center = new Vector3(0, -0.5f, 0);
controller.height = 1;
yield return new WaitForSeconds(1f);
controller.center = new Vector3(0, 0, 0);
controller.height = 2;
animator.SetBool("isSliding", false);
isSliding = false;
}
}