Results 1 to 9 of 9

Thread: 2d Tutorial bug

  1. #1
    Join Date
    Nov 2012
    Posts
    10

    2d Tutorial bug

    Hello working on the 2d tutorial and run into a problem with the Health and Damage script well the script does nothing i take 0 damage

    Code:
    using UnityEngine;
    
    public class HealthBar : MonoBehaviour
    {
        public Player Player;
        public Transform ForegroundSprite;
        public SpriteRenderer ForegroundRenderer;
        public Color MaxHealthColor = new Color(255 / 255f, 63 / 255f, 63 / 255f);
        public Color MinHealthColor = new Color(64 / 255f, 137 / 255f, 255 / 255f);
    
        public void Update()
        {
            var healthPercent = Player.Health / (float) Player.MaxHealth;
    
            ForegroundSprite.localScale = new Vector3(healthPercent, 1, 1);
            ForegroundRenderer.color = Color.Lerp(MaxHealthColor, MinHealthColor, healthPercent);
        }
    }
    Code:
    using UnityEngine;
    
    public class GiveDamageToPlayer : MonoBehaviour
    {
        public int DamageToGive = 10;
    
        private Vector2
            _lastPosition,
            _velocity;
    
        public void LateUpdate()
        {
            _velocity = (_lastPosition - (Vector2) transform.position) / Time.deltaTime;
            _lastPosition = transform.position;
        }
    
        public void OnTiggerEnter2D(Collider2D other)
        {
            var player = other.GetComponent<Player>();
            if (player == null)
                return;
    
            player.TakeDamage(DamageToGive);
            var controller = player.GetComponent<CharacterController2D>();
            var totalVelocity = controller.Velocity + _velocity;
    
            controller.SetForce(new Vector2(
                -1 * Mathf.Sign(totalVelocity.x) * Mathf.Clamp(Mathf.Abs(totalVelocity.x) * 5, 10, 20),
                -1 * Mathf.Sign(totalVelocity.y) * Mathf.Clamp(Mathf.Abs(totalVelocity.y) * 2, 0, 15)));
        }
    }
    Code:
    using UnityEngine;
    using System.Collections;
    
    public class Player : MonoBehaviour {
    	private bool _isFacingRight;
    	private CharacterController2D _controller;
    	private float _normalizedHorizontalSpeed;
    
    	public float MaxSpeed = 8;
    	public float SpeedAcclerationOnGround = 10f;
    	public float SpeedAcclerationInAir = 5f;
        public int MaxHealth = 100;
        public GameObject OuchEffect;
    
        public int Health { get; private set; }
        public bool IsDead { get; private set; }
    	public void Awake() 
        {
    		_controller = GetComponent<CharacterController2D>();
    		_isFacingRight = transform.localScale.x > 0;
    	    Health = MaxHealth;
        }
    
    	public void Update() 
        {
            if (!IsDead)
    		    HandleInput();
    
    		var movementFactor = _controller.State.IsGrounded ? SpeedAcclerationOnGround : SpeedAcclerationInAir;
    
            if (IsDead)
                _controller.SetHorizontalForce(0);
            else
    		_controller.SetHorizontalForce(Mathf.Lerp(_controller.Velocity.x, _normalizedHorizontalSpeed * MaxSpeed, Time.deltaTime * movementFactor));
    	}
    
        public void Kill()
        {
            _controller.HandleCollisions = false;
            collider2D.enabled = false;
            IsDead = true;
            Health = 0;
    
            _controller.SetForce(new Vector2(0, 20));
        }
    
        public void RespawnAt(Transform spawnPoint)
        {
            if (!_isFacingRight)
                Flip();
    
            IsDead = false;
            collider2D.enabled = true;
            _controller.HandleCollisions = true;
            Health = MaxHealth;
    
            transform.position = spawnPoint.position;
        }
    
        public void TakeDamage(int damage)
        {
            Instantiate(OuchEffect, transform.position, transform.rotation);
            Health -= damage;
    
            if (Health <= 0)
                LevelManager.Instance.KillPlayer();
        }
    
    
    	private void HandleInput() {
    		if (Input.GetKey(KeyCode.D)) {
    			_normalizedHorizontalSpeed = 1;
    			if (!_isFacingRight)
    				Flip();
    		}
    				else if (Input.GetKey(KeyCode.A)) {
    					_normalizedHorizontalSpeed = -1;
    					if (_isFacingRight)
    						Flip();
    		}
    		else {
    			_normalizedHorizontalSpeed = 0;
    		}
    
    		if (_controller.CanJump && Input.GetKeyDown(KeyCode.Space)) {
    			_controller.Jump();
    		}
    	}
    
    	private void Flip() {
    		transform.localScale = new Vector3(-transform.localScale.x, transform.localScale.y, transform.localScale.z);
    		_isFacingRight = transform.localScale.x > 0;
    	}
    }

  2. #2
    Join Date
    Nov 2012
    Posts
    10
    Anyone ? i cant find it and would like to keep following the tutorial

  3. #3
    Join Date
    Oct 2014
    Posts
    7
    Hello,

    ur Player Class need to implement the ITakeDamage Interface:

    public class Player : MonoBehaviour , ITakeDamage

    thats all,

    if it wont work now, u may not have assigned the right layer to ur prefabs ( projecile, enemy)

  4. #4
    Join Date
    Oct 2014
    Posts
    7
    Hello,

    ur Player Class need to implement the ITakeDamage Interface:

    public class Player : MonoBehaviour , ITakeDamage

    thats all,

    if it wont work now, u may not have assigned the right layer to ur prefabs ( projecile, enemy)

  5. #5
    Join Date
    Apr 2013
    Posts
    10
    Video #22 health & damage

    All scripts is corrects.
    Inspector platform is
    Layer Default /in Sprite Renderer sorting layer Foreground 0 / BoxCollider2D Trigger flag / script GiveToDamageToplayer attach

    Inspector Player is
    Layer Default
    Sprite Renderer sorting Layer Player 0
    Script attach: Player.cs with Particles / CharacterController2D (platform mask Platforms) / Boxcollider2D no flag trigger

    In the video, the player takes damage and activates the particle Small Red Burst, not me.
    I watched the video 3 times and all scripts are correct.
    My Unity version is 4.5.3
    Thank you for help!

  6. #6
    Join Date
    Apr 2013
    Posts
    10
    I followed video tutorial#22 Health & Damage, but the Player don't take damage from collision with platform.

    Inspector Player is

    Layer DEFAULT

    Sprite Renderer
    Sorting Layer: Player
    Order Layer: 0

    Rigidbody2D

    Box Collider 2D: trigger no flag

    Script component: Player.cs (assigned particles) / Character Controller2D (Platform mask Platforms)


    Inspector Platform is
    Layer DEFAULT

    Sprite Renderer
    Sorting Layer: Foreground
    Order Layer: 0

    Rigidbody 2D
    Kinematic : flag yes

    Box Collider 2D: trigger yes

    Script component: GiveDamageToPlayer

    My Unity Version is 4.5.3
    Thanks for help!

  7. #7
    Join Date
    Apr 2013
    Posts
    10
    Quote Originally Posted by BMigulla View Post
    Hello,

    ur Player Class need to implement the ITakeDamage Interface:

    public class Player : MonoBehaviour , ITakeDamage

    thats all,

    if it wont work now, u may not have assigned the right layer to ur prefabs ( projecile, enemy)
    Your tip does not work.
    Look the Video#22 Health&Damage, the script Player.cs is simply

    public class Player : MonoBehaviour {
    //here code
    }

    In addition with version 4.5.3 pivots are not working correctly.

  8. #8
    Join Date
    Oct 2014
    Posts
    7
    The Layer for Platforms is Platform not Default.
    Otherwise the Player wont collide with it

  9. #9
    Join Date
    Apr 2013
    Posts
    10
    Quote Originally Posted by BMigulla View Post
    The Layer for Platforms is Platform not Default.
    Otherwise the Player wont collide with it
    Another tip wrong, look the Video#22 https://www.youtube.com/watch?v=564B...UD37n&index=22

    The Tutor 3dBuzz change Layer from PLATFORM into DEFAULT.
    If you use the layer PLATFORM, Player collides with the object (flag triggers IS ACTIVE!!), but the main character does not lose life.

    The script GiveDamageToPlayer, HealtBar.cs and Player.cs are ALL corrects!
    I watched the video at least 4-5 times...but I don't understand because in video work and not me.

    The same thing happens to the bullets, and as if the script does not work GiveDamageToPlayer (Video#24 and #25)
    Now my version is Unity Pro 4.5.5

    Thanks for your time.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •