Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by slaga · Oct 03, 2020 at 07:04 AM · animator controllernewbiedeath

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;
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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:

alt text

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


aa1c91a6-467e-4b5c-9a68-b200b490f2a8.png (113.4 kB)
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image slaga · Oct 03, 2020 at 08:54 AM 0
Share

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!

avatar image
0

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??

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

140 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Need Help with Enemy Attack Animation Script? I'm new to Unity. 1 Answer

How do I play death animation in the animator controller properly 1 Answer

I have an attack/take damage script how do I make the player die when health hits 0? 1 Answer

Adding in a system to check for player death. 1 Answer

How do I donwload the sample projects? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges