Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
This question was closed Jul 13, 2018 at 12:23 PM by tormentoarmagedoom for the following reason:

Too subjective and argumentative

avatar image
-1
Question by Sky_Games · Jul 13, 2018 at 01:01 PM · scripting problemerrorscript error

Lives going to negative value

Hello Everyone! I've added 3 lives to my player. But when I get out the 2 lives are taken instead of 1. Example Before Play Lives are 3 Getting out for the first time Lives are 1 Getting out the third time Lives are -1. Please Help!! @seandolan

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;

public class LevelManager : MonoBehaviour {

 public float waitToRespawn;
 public PlayerController thePlayer;

 public GameObject deathSplosion;

 public int coinCount;

 public Text coinText;

 public Image heart1;
 public Image heart2;
 public Image heart3;

 public Sprite heartFull;
 public Sprite heartHalf;
 public Sprite heartEmpty;

 public int maxHealth;
 public int healthCount;

 private bool respawning;

 public ResetOnRespawn[] objectsToReset;

 public bool invincible;

 public Text livesText;
 public int startingLives;
 public int currentLives;

 // Use this for initialization
 void Start () {
     thePlayer = FindObjectOfType<PlayerController> ();

     coinText.text = "Coins: " + coinCount; 

     healthCount = maxHealth;

     objectsToReset = FindObjectsOfType<ResetOnRespawn>();

     currentLives = startingLives;
     livesText.text = "Lives x " + currentLives;
 }
 
 // Update is called once per frame
 void Update () {
     if (healthCount <= 0 && !respawning) 
     {
         Respawn ();
         respawning = true;
     }
 }

 public void Respawn()
 {
     currentLives -= 1;
     livesText.text = "Lives x " + currentLives;

     if (currentLives > 0)
     {
         StartCoroutine ("RespawnCo");
     } else {
         thePlayer.gameObject.SetActive (false);
     }
 }

 public IEnumerator RespawnCo()
 {
     thePlayer.gameObject.SetActive (false);

     Instantiate (deathSplosion, thePlayer.transform.position, thePlayer.transform.rotation);

     yield return new WaitForSeconds (waitToRespawn);

     healthCount = maxHealth;
     respawning = false;
     UpdateHeartMeter ();

     coinCount = 0;
     coinText.text = "Coins: " + coinCount;

     thePlayer.transform.position = thePlayer.respawnPosition;
     thePlayer.gameObject.SetActive (true);

     for (int i = 0; i < objectsToReset.Length; i++) 
     {
         objectsToReset [i].gameObject.SetActive (true);
         objectsToReset [i].ResetObject ();
     }
 }

 public void AddCoins(int coinsToAdd)
 {
     coinCount += coinsToAdd;

     coinText.text = "Coins: " + coinCount;
 }

 public void HurtPlayer(int damageToTake)
 {
     if (!invincible) 
     {    
                 healthCount -= damageToTake;
                 UpdateHeartMeter ();

                 thePlayer.Knockback ();
     }
 }

 public void UpdateHeartMeter()
 {
     switch (healthCount) 
     {
        case 6:
         heart1.sprite = heartFull;
         heart2.sprite = heartFull;
         heart3.sprite = heartFull;
         return;

     case 5:
         heart1.sprite = heartFull;
         heart2.sprite = heartFull;
         heart3.sprite = heartHalf;
         return;

     case 4:
         heart1.sprite = heartFull;
         heart2.sprite = heartFull;
         heart3.sprite = heartEmpty;
         return;

     case 3:
         heart1.sprite = heartFull;
         heart2.sprite = heartHalf;
         heart3.sprite = heartEmpty;
         return;

     case 2:
         heart1.sprite = heartFull;
         heart2.sprite = heartEmpty;
         heart3.sprite = heartEmpty;
         return;

     case 1:
         heart1.sprite = heartHalf;
         heart2.sprite = heartEmpty;
         heart3.sprite = heartEmpty;
         return;

     case 0:
         heart1.sprite = heartEmpty;
         heart2.sprite = heartEmpty;
         heart3.sprite = heartEmpty;
         return;

     default:
         heart1.sprite = heartEmpty;
         heart2.sprite = heartEmpty;
         heart3.sprite = heartEmpty;
         return;
     }
 }

}

using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerController : MonoBehaviour {

 public float moveSpeed; 
 public Rigidbody2D myRigidbody;

 public float jumpSpeed;

 public Transform groundCheck;
 public float groundCheckRadius;
 public LayerMask whatIsGround;

 public bool isGrounded;

 private Animator myAnim;

 private Vector3 initScale;

 public Vector3 respawnPosition;

 public LevelManager theLevelManager;

 public GameObject stompBox;

 public float knockbackForce;
 public float knockbackLength;
 public float knockbackCounter;

 public float invincibilityLength;
 private float invincibilityCounter;

 // Use this for initialization
 void Start ()
 {
     myRigidbody = GetComponent<Rigidbody2D> ();
     myAnim = GetComponent<Animator> ();
     initScale = transform.localScale;

     respawnPosition = transform.position;

     theLevelManager = FindObjectOfType<LevelManager>();
 }
 // Update is called once per frame
 void Update () {
         
     isGrounded = Physics2D.OverlapCircle (groundCheck.position, groundCheckRadius, whatIsGround);

     if (knockbackCounter <= 0) 
     {

                 if (Input.GetAxisRaw ("Horizontal") > 0f) 
                {
                     myRigidbody.velocity = new Vector3 (moveSpeed, myRigidbody.velocity.y, 0f);
                     transform.localScale = new Vector3 (1f, 1f, 1f);
                     transform.localScale = new Vector3 (initScale.x, initScale.y, initScale.z);
                 } else if (Input.GetAxisRaw ("Horizontal") < 0f) {
                     myRigidbody.velocity = new Vector3 (-moveSpeed, myRigidbody.velocity.y, 0f);
                     transform.localScale = new Vector3 (-1f, 1f, 1f);
                     transform.localScale = new Vector3 (-initScale.x, initScale.y, initScale.z);
                 } else {
                     myRigidbody.velocity = new Vector3 (0f, myRigidbody.velocity.y, 0f);
                 }

                 if (Input.GetButtonDown ("Jump") && isGrounded)
         {
                     myRigidbody.velocity = new Vector3 (myRigidbody.velocity.x, jumpSpeed, 0f);
                 }
             
     }

     if (knockbackCounter > 0) 
     {
         knockbackCounter -= Time.deltaTime;

         if (transform.localScale.x > 0) 
         {
             myRigidbody.velocity = new Vector3 (-knockbackForce, knockbackForce, 0f);
         } else {
             myRigidbody.velocity = new Vector3 (knockbackForce, knockbackForce, 0f);
         }
     }

     if (invincibilityCounter > 0) 
     {
         invincibilityCounter -= Time.deltaTime;
     }

     if (invincibilityCounter <= 0) 
     {
         theLevelManager.invincible = false;
     }

     myAnim.SetFloat ("Speed", Mathf.Abs (myRigidbody.velocity.x));
     myAnim.SetBool ("Grounded", isGrounded);

     if (myRigidbody.velocity.y < 0) 
     {
         stompBox.SetActive(true);
     }else {
         stompBox.SetActive (false);
         }
 }

 public void Knockback()
 {
     knockbackCounter = knockbackLength;
     invincibilityCounter = invincibilityLength;
     theLevelManager.invincible = true;
 }


 void OnTriggerEnter2D(Collider2D other)
 {
     if (other.tag == "KillPlane") 
     {
         //gameObject.SetActive (false);

         //transform.position = respawnPosition;

         theLevelManager.Respawn ();
     }

     if (other.tag == "Checkpoint") 
     {
         respawnPosition = other.transform.position;
     }
 }

 void OnCollisionEnter2D(Collision2D other)
 {
     if (other.gameObject.tag == "MovingPlatform") 
     {
         transform.parent = other.transform;
     }
 }

 void OnCollisionExit2D(Collision2D other)
 {
     if (other.gameObject.tag == "MovingPlatform") 
     {
         transform.parent = null;
     }
 }

}

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

3 Replies

  • Sort: 
avatar image
0

Answer by kalen_08 · Jul 11, 2018 at 04:35 AM

I don't know why you are losing 2 lives but to prevent the lives from going negative use:

 lives = Mathf.Clamp(lives - 1, 0, lives)

this makes sure the value is between the min (0) and max (lives) values.

EDIT: It still is going down by 2 or is it still going negative?

  1. Double click currentLives and then push shift + F12 to see all the uses of the field.

  2. Print when you are changing the values.

      Debug.Log ("lives before = " + currentLives);
         currentLives = Mathf.Clamp (currentLives, 0, 100);
         Debug.Log ("lives after = " + currentLives);
     
    
    

Have the console open when testing that way you can see if it is taking 2 away in quick succession because for two frames it doesn't know it's dead yet.

EDIT: See if setting a range on the initialization of the variable prevents this.

 [SerializeField] [Range (0,100] private int lives;
 
 public int Lives {
       get { return lives }
       set { lives = Mathf.Clamp (value, 0, 100)
 }
 
 public void AddLives (int livesToAdd)
 {
       Lives += livesToAdd;
 }


ALL CHANGES to the lives must use the property, this will ensure the value is always clamped, also there is the Range attribute on the field, definitely do the shift + F12 to see all references to the field first though, it's probably being called from somewhere else, making it private will keep it from being changed from outside the script except through the property which will clamp the value Copy and paste the code above into your script. Fingers crossed this works :)

Comment
Add comment · Show 8 · 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 Sky_Games · Jul 11, 2018 at 01:49 PM 0
Share

Hey $$anonymous$$alen! Thanks for your reply but it didn't worked! I think there is a problem related to killplane but don't know for sure :( Thanks, Ishan

avatar image Sky_Games · Jul 12, 2018 at 01:46 PM 0
Share

Hello $$anonymous$$alen, The lives are going down as 3,1,-1 :(

avatar image Sky_Games · Jul 12, 2018 at 04:04 PM 0
Share

Hey $$anonymous$$alen, the issue is 90% solved. Now it's 3,2,1,-1

avatar image Sky_Games Sky_Games · Jul 12, 2018 at 04:07 PM 0
Share

I'm posting the code again so that you can figure out what's the issue:

avatar image kalen_08 Sky_Games · Jul 12, 2018 at 11:12 PM 0
Share

Hmm ins$$anonymous$$d of checking if he's dead in Update, check after he takes damage.

Show more comments
avatar image kalen_08 Sky_Games · Jul 13, 2018 at 10:06 PM 0
Share

Yes, I've looked at your new code and it's the same as the old code... Put the Range attribute on as in my code above in my answer

 [Range (0, 100)] public int current lives;
 
 Public void SetLives (int value)
 {
       lives = $$anonymous$$athf.Clamp (value, 0, 100);
 }
 
 
 Public void TakeLives ()
 {
       lives = $$anonymous$$athf.Clamp (lives - 1, 0, 100);
 }

  • Copy

  • Paste

  • Use the functions

avatar image kalen_08 · Jul 21, 2018 at 03:23 PM 0
Share

If this helped you then upvote it and mark it as the accepted answer. If not then your on your own. You have done literally nothing that I said to do. The code you post again is the same as the original. If you ask for help then take the advice, don't keep asking but not even using the help.

The problem hasn't been fixed because you've done nothing to fix it.

please and thanks. :)

avatar image
0

Answer by Sky_Games · Jul 13, 2018 at 06:26 AM

@kalen_08

public class PlayerController : MonoBehaviour {

 public float moveSpeed; 
 public Rigidbody2D myRigidbody;

 public float jumpSpeed;

 public Transform groundCheck;
 public float groundCheckRadius;
 public LayerMask whatIsGround;

 public bool isGrounded;

 private Animator myAnim;

 private Vector3 initScale;

 public Vector3 respawnPosition;

 public LevelManager theLevelManager;

 public GameObject stompBox;

 public float knockbackForce;
 public float knockbackLength;
 public float knockbackCounter;

 public float invincibilityLength;
 private float invincibilityCounter;

 // Use this for initialization
 void Start ()
 {
     myRigidbody = GetComponent<Rigidbody2D> ();
     myAnim = GetComponent<Animator> ();
     initScale = transform.localScale;

     respawnPosition = transform.position;

     theLevelManager = FindObjectOfType<LevelManager>();
 }
 // Update is called once per frame
 void Update () {
         
     isGrounded = Physics2D.OverlapCircle (groundCheck.position, groundCheckRadius, whatIsGround);

     if (knockbackCounter <= 0) 
     {

                 if (Input.GetAxisRaw ("Horizontal") > 0f) 
                {
                     myRigidbody.velocity = new Vector3 (moveSpeed, myRigidbody.velocity.y, 0f);
                     transform.localScale = new Vector3 (1f, 1f, 1f);
                     transform.localScale = new Vector3 (initScale.x, initScale.y, initScale.z);
                 } else if (Input.GetAxisRaw ("Horizontal") < 0f) {
                     myRigidbody.velocity = new Vector3 (-moveSpeed, myRigidbody.velocity.y, 0f);
                     transform.localScale = new Vector3 (-1f, 1f, 1f);
                     transform.localScale = new Vector3 (-initScale.x, initScale.y, initScale.z);
                 } else {
                     myRigidbody.velocity = new Vector3 (0f, myRigidbody.velocity.y, 0f);
                 }

                 if (Input.GetButtonDown ("Jump") && isGrounded)
         {
                     myRigidbody.velocity = new Vector3 (myRigidbody.velocity.x, jumpSpeed, 0f);
                 }
             
     }

     if (knockbackCounter > 0) 
     {
         knockbackCounter -= Time.deltaTime;
         if (transform.localScale.x > 0) 
         {
             myRigidbody.velocity = new Vector3 (-knockbackForce,knockbackForce, 0f);
         } else {
             myRigidbody.velocity = new Vector3 (knockbackForce, knockbackForce, 0f);
         }
     }
     if (invincibilityCounter > 0) 
     {
         invincibilityCounter -= Time.deltaTime;
     }
     if (invincibilityCounter <= 0) 
     {
         theLevelManager.invincible = false;
     }
     myAnim.SetFloat ("Speed", Mathf.Abs (myRigidbody.velocity.x));
     myAnim.SetBool ("Grounded", isGrounded);
     if (myRigidbody.velocity.y < 0) 
     {
         stompBox.SetActive(true);
     }else {
         stompBox.SetActive (false);
         }
 }
 public void Knockback()
 {
     knockbackCounter = knockbackLength;
     invincibilityCounter = invincibilityLength;
     theLevelManager.invincible = true;
 }

 void OnTriggerEnter2D(Collider2D other)
 {
     if (other.tag == "KillPlane") 
     {
         //gameObject.SetActive (false);

         //transform.position = respawnPosition;

         theLevelManager.Respawn ();
         //theLevelManager.healthCount = 0;
     }

     if (other.tag == "Checkpoint") 
     {
         respawnPosition = other.transform.position;
     }
 }

 void OnCollisionEnter2D(Collision2D other)
 {
     if (other.gameObject.tag == "MovingPlatform") 
     {
         transform.parent = other.transform;
     }
 }
 void OnCollisionExit2D(Collision2D other)
 {
     if (other.gameObject.tag == "MovingPlatform") 
     {
         transform.parent = null;
     }
 }

}

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;

public class LevelManager : MonoBehaviour {

 public float waitToRespawn;
 public PlayerController thePlayer;

 public GameObject deathSplosion;

 public int coinCount;

 public Text coinText;

 public Image heart1;
 public Image heart2;
 public Image heart3;

 public Sprite heartFull;
 public Sprite heartHalf;
 public Sprite heartEmpty;

 public int maxHealth;
 public int healthCount;

 private bool respawning;

 public ResetOnRespawn[] objectsToReset;

 public bool invincible;

 public Text livesText;
 public int startingLives;
 public int currentLives;

 // Use this for initialization
 void Start () {
     thePlayer = FindObjectOfType<PlayerController> ();

     coinText.text = "Coins: " + coinCount; 

     healthCount = maxHealth;

     objectsToReset = FindObjectsOfType<ResetOnRespawn>();

     currentLives = startingLives;
     livesText.text = "Lives x " + currentLives;
 }
 
 // Update is called once per frame
 void Update () {
     if (healthCount <= 0 ) 
     {
         Respawn ();
         respawning = true;
     }
 }

 public void Respawn()
 {
     if (!respawning) {
         currentLives -= 1;
         livesText.text = "Lives x " + currentLives;

         if (currentLives > 0) {
             respawning = true;
             StartCoroutine ("RespawnCo");
         } else {
             thePlayer.gameObject.SetActive (false);
         }
     }
 }

 public IEnumerator RespawnCo()
 {
     thePlayer.gameObject.SetActive (false);

     Instantiate (deathSplosion, thePlayer.transform.position, thePlayer.transform.rotation);

     yield return new WaitForSeconds (waitToRespawn);

     healthCount = maxHealth;
     respawning = false;
     UpdateHeartMeter ();

     coinCount = 0;
     coinText.text = "Coins: " + coinCount;

     thePlayer.transform.position = thePlayer.respawnPosition;
     thePlayer.gameObject.SetActive (true);

     for (int i = 0; i < objectsToReset.Length; i++) 
     {
         objectsToReset [i].gameObject.SetActive (true);
         objectsToReset [i].ResetObject ();
     }
 }

 public void AddCoins(int coinsToAdd)
 {
     coinCount += coinsToAdd;

     coinText.text = "Coins: " + coinCount;
 }

 public void HurtPlayer(int damageToTake)
 {
     if (!invincible) 
     {    
                 healthCount -= damageToTake;
                 UpdateHeartMeter ();

                 thePlayer.Knockback ();
     }
 }

 public void UpdateHeartMeter()
 {
     switch (healthCount) 
     {
        case 6:
         heart1.sprite = heartFull;
         heart2.sprite = heartFull;
         heart3.sprite = heartFull;
         return;

     case 5:
         heart1.sprite = heartFull;
         heart2.sprite = heartFull;
         heart3.sprite = heartHalf;
         return;

     case 4:
         heart1.sprite = heartFull;
         heart2.sprite = heartFull;
         heart3.sprite = heartEmpty;
         return;

     case 3:
         heart1.sprite = heartFull;
         heart2.sprite = heartHalf;
         heart3.sprite = heartEmpty;
         return;

     case 2:
         heart1.sprite = heartFull;
         heart2.sprite = heartEmpty;
         heart3.sprite = heartEmpty;
         return;

     case 1:
         heart1.sprite = heartHalf;
         heart2.sprite = heartEmpty;
         heart3.sprite = heartEmpty;
         return;

     case 0:
         heart1.sprite = heartEmpty;
         heart2.sprite = heartEmpty;
         heart3.sprite = heartEmpty;
         return;

     default:
         heart1.sprite = heartEmpty;
         heart2.sprite = heartEmpty;
         heart3.sprite = heartEmpty;
         return;
     }
 }

}

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
avatar image
0

Answer by Sky_Games · Jul 13, 2018 at 06:26 AM

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;

public class LevelManager : MonoBehaviour {

 public float waitToRespawn;
 public PlayerController thePlayer;

 public GameObject deathSplosion;

 public int coinCount;

 public Text coinText;

 public Image heart1;
 public Image heart2;
 public Image heart3;

 public Sprite heartFull;
 public Sprite heartHalf;
 public Sprite heartEmpty;

 public int maxHealth;
 public int healthCount;

 private bool respawning;

 public ResetOnRespawn[] objectsToReset;

 public bool invincible;

 public Text livesText;
 public int startingLives;
 public int currentLives;

 // Use this for initialization
 void Start () {
     thePlayer = FindObjectOfType<PlayerController> ();

     coinText.text = "Coins: " + coinCount; 

     healthCount = maxHealth;

     objectsToReset = FindObjectsOfType<ResetOnRespawn>();

     currentLives = startingLives;
     livesText.text = "Lives x " + currentLives;
 }
 
 // Update is called once per frame
 void Update () {
     if (healthCount <= 0 ) 
     {
         Respawn ();
         respawning = true;
     }
 }

 public void Respawn()
 {
     if (!respawning) {
         currentLives -= 1;
         livesText.text = "Lives x " + currentLives;

         if (currentLives > 0) {
             respawning = true;
             StartCoroutine ("RespawnCo");
         } else {
             thePlayer.gameObject.SetActive (false);
         }
     }
 }

 public IEnumerator RespawnCo()
 {
     thePlayer.gameObject.SetActive (false);

     Instantiate (deathSplosion, thePlayer.transform.position, thePlayer.transform.rotation);

     yield return new WaitForSeconds (waitToRespawn);

     healthCount = maxHealth;
     respawning = false;
     UpdateHeartMeter ();

     coinCount = 0;
     coinText.text = "Coins: " + coinCount;

     thePlayer.transform.position = thePlayer.respawnPosition;
     thePlayer.gameObject.SetActive (true);

     for (int i = 0; i < objectsToReset.Length; i++) 
     {
         objectsToReset [i].gameObject.SetActive (true);
         objectsToReset [i].ResetObject ();
     }
 }

 public void AddCoins(int coinsToAdd)
 {
     coinCount += coinsToAdd;

     coinText.text = "Coins: " + coinCount;
 }

 public void HurtPlayer(int damageToTake)
 {
     if (!invincible) 
     {    
                 healthCount -= damageToTake;
                 UpdateHeartMeter ();

                 thePlayer.Knockback ();
     }
 }

 public void UpdateHeartMeter()
 {
     switch (healthCount) 
     {
        case 6:
         heart1.sprite = heartFull;
         heart2.sprite = heartFull;
         heart3.sprite = heartFull;
         return;

     case 5:
         heart1.sprite = heartFull;
         heart2.sprite = heartFull;
         heart3.sprite = heartHalf;
         return;

     case 4:
         heart1.sprite = heartFull;
         heart2.sprite = heartFull;
         heart3.sprite = heartEmpty;
         return;

     case 3:
         heart1.sprite = heartFull;
         heart2.sprite = heartHalf;
         heart3.sprite = heartEmpty;
         return;

     case 2:
         heart1.sprite = heartFull;
         heart2.sprite = heartEmpty;
         heart3.sprite = heartEmpty;
         return;

     case 1:
         heart1.sprite = heartHalf;
         heart2.sprite = heartEmpty;
         heart3.sprite = heartEmpty;
         return;

     case 0:
         heart1.sprite = heartEmpty;
         heart2.sprite = heartEmpty;
         heart3.sprite = heartEmpty;
         return;

     default:
         heart1.sprite = heartEmpty;
         heart2.sprite = heartEmpty;
         heart3.sprite = heartEmpty;
         return;
     }
 }

}

using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerController : MonoBehaviour {

 public float moveSpeed; 
 public Rigidbody2D myRigidbody;

 public float jumpSpeed;

 public Transform groundCheck;
 public float groundCheckRadius;
 public LayerMask whatIsGround;

 public bool isGrounded;

 private Animator myAnim;

 private Vector3 initScale;

 public Vector3 respawnPosition;

 public LevelManager theLevelManager;

 public GameObject stompBox;

 public float knockbackForce;
 public float knockbackLength;
 public float knockbackCounter;

 public float invincibilityLength;
 private float invincibilityCounter;

 // Use this for initialization
 void Start ()
 {
     myRigidbody = GetComponent<Rigidbody2D> ();
     myAnim = GetComponent<Animator> ();
     initScale = transform.localScale;

     respawnPosition = transform.position;

     theLevelManager = FindObjectOfType<LevelManager>();
 }
 // Update is called once per frame
 void Update () {
         
     isGrounded = Physics2D.OverlapCircle (groundCheck.position, groundCheckRadius, whatIsGround);

     if (knockbackCounter <= 0) 
     {

                 if (Input.GetAxisRaw ("Horizontal") > 0f) 
                {
                     myRigidbody.velocity = new Vector3 (moveSpeed, myRigidbody.velocity.y, 0f);
                     transform.localScale = new Vector3 (1f, 1f, 1f);
                     transform.localScale = new Vector3 (initScale.x, initScale.y, initScale.z);
                 } else if (Input.GetAxisRaw ("Horizontal") < 0f) {
                     myRigidbody.velocity = new Vector3 (-moveSpeed, myRigidbody.velocity.y, 0f);
                     transform.localScale = new Vector3 (-1f, 1f, 1f);
                     transform.localScale = new Vector3 (-initScale.x, initScale.y, initScale.z);
                 } else {
                     myRigidbody.velocity = new Vector3 (0f, myRigidbody.velocity.y, 0f);
                 }

                 if (Input.GetButtonDown ("Jump") && isGrounded)
         {
                     myRigidbody.velocity = new Vector3 (myRigidbody.velocity.x, jumpSpeed, 0f);
                 }
             
     }

     if (knockbackCounter > 0) 
     {
         knockbackCounter -= Time.deltaTime;

         if (transform.localScale.x > 0) 
         {
             myRigidbody.velocity = new Vector3 (-knockbackForce, knockbackForce, 0f);
         } else {
             myRigidbody.velocity = new Vector3 (knockbackForce, knockbackForce, 0f);
         }
     }

     if (invincibilityCounter > 0) 
     {
         invincibilityCounter -= Time.deltaTime;
     }

     if (invincibilityCounter <= 0) 
     {
         theLevelManager.invincible = false;
     }

     myAnim.SetFloat ("Speed", Mathf.Abs (myRigidbody.velocity.x));
     myAnim.SetBool ("Grounded", isGrounded);

     if (myRigidbody.velocity.y < 0) 
     {
         stompBox.SetActive(true);
     }else {
         stompBox.SetActive (false);
         }
 }

 public void Knockback()
 {
     knockbackCounter = knockbackLength;
     invincibilityCounter = invincibilityLength;
     theLevelManager.invincible = true;
 }


 void OnTriggerEnter2D(Collider2D other)
 {
     if (other.tag == "KillPlane") 
     {
         //gameObject.SetActive (false);

         //transform.position = respawnPosition;

         theLevelManager.Respawn ();
         //theLevelManager.healthCount = 0;
     }

     if (other.tag == "Checkpoint") 
     {
         respawnPosition = other.transform.position;
     }
 }

 void OnCollisionEnter2D(Collision2D other)
 {
     if (other.gameObject.tag == "MovingPlatform") 
     {
         transform.parent = other.transform;
     }
 }

 void OnCollisionExit2D(Collision2D other)
 {
     if (other.gameObject.tag == "MovingPlatform") 
     {
         transform.parent = null;
     }
 }

}

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

Follow this Question

Answers Answers and Comments

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

Referencing another script error/problem 0 Answers

error CS1526: A new expression requires () or [] after type 1 Answer

error CS1525: Unexpected symbol `' 2 Answers

BCE0044: expecting ''', found '\r'. 1 Answer

(31,16): error CS1525: Unexpected symbol `(', expecting `)', `,', `;', `[', or `=' 2 Answers


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