- Home /
My problem has been resolved.
How to get multiple extra lives by collision
Hi, everyone.
I have a problem and I don't remember how to solve it. I designed this game where if you pass the goal at a certain amount of time, then you earn five hundred bonus points and, if you pass an increment of five hundred points, you earn five extra lives. Otherwise, if you pass a hundred points after passing the goal, then you earn points based on the time left on the clock. Also, I posted the Shuriken Controller class. Can anyone help with editing the following code? I know it's a lot, but any assistance would be appreciated.
The GameStatus class controls the score and lives counters.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameStatus : MonoBehaviour
{
static public int score = 0;
static public int lives = 9;
public static int target;
public const int targetInterval = 100;
public AudioSource myAudioSource;
public AudioClip bell;
// Start is called before the first frame update
void Start()
{
target = targetInterval;
myAudioSource = GetComponent<AudioSource>();
}
void OnDestroy()
{
Debug.Log("GameStatus was destroyed.");
}
public static int GetScore()
{
return score;
}
public static int GetLives()
{
return lives;
}
public void AddScore(int pts)
{
score += pts;
if (score >= target)
{
lives++;
target += targetInterval;
myAudioSource.PlayOneShot(bell);
}
}
}
The PlayerController class speaks for itself, and is the main part of my problem.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using System;
using System.IO;
public class PlayerController : MonoBehaviour
{
public Rigidbody2D myBod;
public AudioSource myAudioSource;
public int moveOnDelay = 4;
public AudioClip crowd;
public AudioClip cent;
public AudioClip net;
public AudioClip bell;
public AudioClip splash;
public AudioClip bat;
public CountdownTimer ct;
public Vector3 respawnPoint;
private UnitManager gameUM;
private HealthManager hm;
public AudioClip takeSound;
public int damageTaken = 1;
public Transform firePoint;
public GameObject ninjaStar;
public GameStatus gs;
// Start is called before the first frame update
void Start()
{
myBod = GetComponent<Rigidbody2D>();
ct = FindObjectOfType<CountdownTimer>();
myAudioSource = GetComponent<AudioSource>();
gameUM = FindObjectOfType<UnitManager>();
hm = FindObjectOfType<HealthManager>();
respawnPoint = transform.position;
gs = FindObjectOfType<GameStatus>();
if (GlobalControl.Instance.IsSceneBeingLoaded)
{
PlayerState.Instance.localPlayerData = GlobalControl.Instance.LocalCopyOfData;
transform.position = new Vector3(
GlobalControl.Instance.LocalCopyOfData.PositionX,
GlobalControl.Instance.LocalCopyOfData.PositionY,
GlobalControl.Instance.LocalCopyOfData.PositionZ + 0.1f);
GlobalControl.Instance.IsSceneBeingLoaded = false;
}
if (Input.GetKeyDown(KeyCode.Return))
{
Instantiate(ninjaStar, firePoint.position, firePoint.rotation);
}
}
// Update is called once per frame
void Update()
{
float x = Input.GetAxis("Horizontal");
Vector2 origin = Vector2.zero;
if (Input.GetButtonDown("Jump"))
{
myBod.velocity = new Vector2(x * 5, 7f);
}
else
{
myBod.velocity = new Vector2(x * 5, myBod.velocity.y);
}
if (Input.GetKeyDown(KeyCode.Return))
{
Instantiate(ninjaStar, firePoint.position, firePoint.rotation);
}
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.tag == "coin")
{
Destroy(other.gameObject);
gs.AddScore(10);
myAudioSource.PlayOneShot(cent);
}
if (other.tag == "obstacle" || other.gameObject.tag == "enemy")
{
HealthManager.HurtPlayer(damageTaken);
myAudioSource.PlayOneShot(takeSound);
}
if (other.tag == "FallDetector")
{
gameUM.LifeLost();
myAudioSource.PlayOneShot(splash);
}
if (other.tag == "bottle")
{
hm.FullHealth();
myAudioSource.PlayOneShot(crowd);
}
if (other.tag == "goal")
{
if (ct.countingTime > 10f)
{
int bonusScore = (int)ct.countingTime * 2;
gs.AddScore(bonusScore);
if (GameStatus.score < GameStatus.target)
myAudioSource.PlayOneShot(net);
}
else
{
ct.myAudioSource.Stop();
int bonusPoints = 500;
gs.AddScore(bonusPoints);
}
StartCoroutine("MoveToNextUnit");
}
}
public void Save()
{
PlayerState.Instance.localPlayerData.SceneID = SceneManager.GetActiveScene().buildIndex;
PlayerState.Instance.localPlayerData.PositionX = transform.position.x;
PlayerState.Instance.localPlayerData.PositionY = transform.position.y;
PlayerState.Instance.localPlayerData.PositionZ = transform.position.z;
PlayerState.Instance.localPlayerData.score = GameStatus.score;
PlayerState.Instance.localPlayerData.lives = GameStatus.lives;
GlobalControl.Instance.SaveData();
}
public void Load()
{
GlobalControl.Instance.LoadData();
GlobalControl.Instance.IsSceneBeingLoaded = true;
int whichScene = GlobalControl.Instance.LocalCopyOfData.SceneID;
SceneManager.LoadScene(whichScene);
this.transform.position = new Vector3(GlobalControl.Instance.LocalCopyOfData.PositionX, GlobalControl.Instance.LocalCopyOfData.PositionY, GlobalControl.Instance.LocalCopyOfData.PositionZ);
GameStatus.score = GlobalControl.Instance.LocalCopyOfData.score;
GameStatus.lives = GlobalControl.Instance.LocalCopyOfData.lives;
Debug.Log("Saved Position: (" + this.transform.position + ")");
Debug.Log("Saved Stage: " + whichScene);
}
public IEnumerator MoveToNextUnit()
{
yield return new WaitForSeconds(moveOnDelay);
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}
}
The ShurikenController is how the player will defend itself against their enemies to gain even more points.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ShurikenController : MonoBehaviour
{
public Rigidbody2D star;
public float speed;
public PlayerController player;
public GameObject enemyDestructEffect;
public GameObject impactEffect;
public int pointsForDestroy = 20;
public AudioSource audioSource;
public AudioClip shot;
public AudioClip poof;
public AudioClip bell;
public float musicLength = 0.25f;
// Start is called before the first frame update
void Start()
{
audioSource = GetComponent<AudioSource>();
player = FindObjectOfType<PlayerController>();
star = GetComponent<Rigidbody2D>();
if (player.transform.localScale.x < 0)
{
speed = -speed;
}
}
// Update is called once per frame
void Update()
{
star.velocity = new Vector2(speed, star.velocity.y);
audioSource.PlayOneShot(shot);
StartCoroutine("StopAudio");
}
void OnTriggerEnter2D(Collider2D other)
{
Instantiate(impactEffect, transform.position, transform.rotation);
if (other.gameObject.tag == "enemy")
{
Destroy(other.gameObject);
Destroy(gameObject);
AudioSource.PlayClipAtPoint(poof, transform.position);
GameStatus.score += 20;
}
}
public IEnumerator StopAudio()
{
yield return new WaitForSeconds(musicLength);
audioSource.Stop();
}
}
Can anyone help with this? I know this is a lot of code. Still, any assistance would be appreciated.
Sincerely, burchland2
Answer by unity_hosein · Nov 24, 2021 at 04:05 AM
when you are attaching 2 or more object to colliders the collision return the nearest object
I don't understand what you mean. Can you please explain or give me an example?
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Why isn't my high score sticking? 2 Answers
Making a 3d text object represent the "health" of an object? 1 Answer