- Home /
Kinda lost with animating player from another script
Hey everyone noob question but so am i! Anyways i am trying to make a game and trying to find my own way around. so i got to a point i implement lives to my character and in my gamemanager i want to say when lives <= 0 game over and play the players death animation. I ised to have the death animation in the collider but now since i want to have lives i dont know how to make it work.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class GameManager : MonoBehaviour
{
public static GameManager instance;
public GameObject PlayerDeath;
public GameObject livesHolder;
public GameObject gameOverPanel;
public Text scoreText;
int score = 0;
int lives = 5;
// Start is called before the first frame update
private void Start()
{
gameOverPanel.SetActive(false);
}
private void Awake()
{
if (instance == null)
{
instance = this;
}
}
// Update is called once per frame
void Update()
{
}
public void GameOver()
{
ObstacleSpawner.instance.gameOver = true;
StopScrolling();
gameOverPanel.SetActive(true);
}
void StopScrolling()
{
TextureScroll[] scrollingObjects = FindObjectsOfType<TextureScroll>();
foreach (TextureScroll t in scrollingObjects)
{
t.scroll = false;
}
}
public void Restart()
{
SceneManager.LoadScene("Game");
}
public void Menu()
{
SceneManager.LoadScene("Menu");
}
public void IncrementScore()
{
score ++;
if(scoreText == null) Debug.LogError("scoreText not provided, click this message", gameObject);
else scoreText.text = score.ToString("0");
}
public void DecreaseLife()
{
if (lives > 0)
{
lives--;
livesHolder.transform.GetChild(lives).gameObject.SetActive(false);
}
else if (lives <= 0)
{
//PlayerController Death = PlayerDeath.GetComponent<Death>();
PlayerController.GetComponent.Death();
PlayerController.Death();
//GameManager.instance.GameOver();
//GameOver();
}
}
}
this was my gamemanager and here is my playercontroller
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
Rigidbody2D rb;
public Animator anim;
bool grounded;
public bool gameOver = false;
[SerializeField]
float jumpForce;
// Start is called before the first frame update
private void Awake()
{
rb = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0) && !gameOver)
if (grounded)
{
{
Jump();
}
}
}
void Jump()
{
grounded = false;
rb.velocity = Vector2.up * jumpForce;
anim.SetTrigger("Jump");
GameManager.instance.IncrementScore();
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "Ground")
{
grounded = true;
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
if(collision.gameObject.tag == "Obstacle")
{
Destroy(collision.gameObject);
GameManager.instance.DecreaseLife();
}
}
public void Death()
{
anim.Play("PlayerDie");
GameManager.instance.GameOver();
gameOver = true;
}
}
Answer by UnityToMakeMoney · Oct 03, 2020 at 07:47 AM
I will be 100% honest with you, your code looks somewhat organized (much better than most beginners), but from a top-down level design perspective it looks messy. First, keep all your player's movement into 1 script. Second, I would also keep all your animation changes into another script. Lastly, I would create a script that manages both the player's movement and animation through a script.
Something like this:
Things like lives would be in the PlayerController script because it has nothing to do with movement or animations. Same goes for collisions.
Using Object-Oriented Principles, we can communicate what we need to do through public functions, that way when the player is hit we can communicate to the AnimationController to play a specific animation based on the lives.
Here is an example:
/*This is all within a player controller script*/
private void isDead = false;
public int lives = 3;
AnimController ac;//short hand for script
PlayerMovement pm;//short hand for player movement
void Start(){
ac = GetComponent<AnimController>();//get the script
pm = GetComponent<PlayerMovement>();//get the script
}
void Update(){
//we would check movement script
//we would check what animation to play based on movement
if(pm.isMovingRight){
ac.PlayMoveRight();//plays moving right animation
}
}
//checks collisions
private void OnCollisionEnter2D(Collider2D col){
if(col.gameObject.tag == "enemy")
Hurt();
}
private void Hurt(){
lives--;//decrease lives
if(lives > 0){
ac.PlayHurt();//call script's method to play hurt animation
}else{
//dead
ac.PlayDeath();//call script's method to play death animation
isDead = true;
}
}
//used for GameManager
public bool isDead(){
return isDead;//return boolean flag variable
}
/*This way, the game manager can check if the player is dead*/
If you need help reorganizing, let me know, I will walk you through it. @slaga
thank you well i am a beginner in unity i do use game maker studio and i am a backend developer just i dont know c# that well. i was thinking of making a script that handles most of the things so i wouldn't put it all in my player. thats why i started writing a game manager script that would handle all of those things and that where things got messy!in python for instance you can reference another script by its location and importing the function and to be honest i dont know how to do that in c#! it seems a bit more difficult than ruby /python but hey im learning! thank you for the reply , thats how i did it to. kept the game manager script to handle game over, scene transistions and the score!
Answer by slaga · Oct 03, 2020 at 07:42 AM
i did make it happen by putting everything into the playercontroller but just for refereance if i wanted to have it in another script how would i go about doing that??