Wednesday, April 27, 2016

Part 5b - Enemy Combat and Death

See the previous parts here:
Check out the code below the videos




In part 5b of this Unity3D tutorial we add a way for the player to kill the enemy. The player will be able to jump on top of an enemy to kill it and bounce up.
Twitter: https://twitter.com/Devination3D
Google+ Community: https://plus.google.com/communities/108584850180626452949
Facebook: https://www.facebook.com/unity3Dtutorialsbydevin

Time Breakdown:
0:33 - Adding the Hurt function to our Enemy
1:47 - Check contact point(s) between Enemy and Player
4:04 - Visualizing our contact points and normals in game
5:13 - Changing player colliders to work properly
6:25 - Explaination of how we will check to see if the Player or the Enemy should get hurt
7:50 - Demonstration of Enemy kill code working
8:08 - Adding small jump force when killing an Enemy
9:04 - Demonstration of pop up code working



Support Further Tutorial Development
Comments?
Check out the videos above to hear the explanation and see the code in action

PlayerController.cs
/*/
* Script by Devin Curry
* www.Devination.com
* www.youtube.com/user/curryboy001
* Please like and subscribe if you found my tutorials helpful :D
* Twitter: https://twitter.com/Devination3D
/*/
using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour
{
 public static PlayerController instance;

 //Movement
 public float speed = 10, jumpVelocity = 10;
 public LayerMask playerMask;
 public bool canMoveInAir = true;

 //Combat
 public int health = 3;
 public float invincibleTimeAfterHurt = 2;

 [HideInInspector]
 public Collider2D[] myColls;

 Transform myTrans, tagGround;
 Rigidbody2D myBody;
 bool isGrounded = false;
 float hInput;
 AnimatorController myAnim;

 void Start ()
 {
  instance = this;
  myColls = this.GetComponents();
  myBody = this.GetComponent();
  myTrans = this.transform;
  tagGround = GameObject.Find (this.name + "/tag_ground").transform;
  myAnim = AnimatorController.instance;
 }
 void FixedUpdate ()
 {
  isGrounded = Physics2D.Linecast (myTrans.position, tagGround.position, playerMask);
  myAnim.UpdateIsGrounded (isGrounded);
  #if !UNITY_ANDROID && !UNITY_IPHONE && !UNITY_BLACKBERRY && !UNITY_WINRT || UNITY_EDITOR
  hInput = Input.GetAxisRaw("Horizontal");
  myAnim.UpdateSpeed(hInput);
  if(Input.GetButtonDown("Jump"))
   Jump();
  #endif
  
  Move(hInput);
 }
 void Move(float horizonalInput)
 {
  if(!canMoveInAir && !isGrounded)
   return;
  Vector2 moveVel = myBody.velocity;
  moveVel.x = horizonalInput * speed;
  myBody.velocity = moveVel;
 }
 public void Jump()
 {
  if(isGrounded)
   myBody.velocity += jumpVelocity * Vector2.up;
 }
 public void StartMoving(float horizonalInput)
 {
  hInput = horizonalInput;
  myAnim.UpdateSpeed(horizonalInput);
 }

 void Hurt()
 {
  health--;
  if (health <= 0)
   Application.LoadLevel(Application.loadedLevel);
  else
   myAnim.TriggerHurt(invincibleTimeAfterHurt);
 }
 void OnCollisionEnter2D(Collision2D collision)
 {
  Enemy enemy = collision.collider.GetComponent();
  if(enemy != null)
  {
   foreach(ContactPoint2D point in collision.contacts)
   {
    Debug.Log(point.normal);
    Debug.DrawLine(point.point, point.point + point.normal, Color.red, 10);
    if( point.normal.y >= 0.9f )
    {
     Vector2 velocity = myBody.velocity;
     velocity.y = jumpVelocity;
     myBody.velocity = velocity;
     enemy.Hurt();
    }
    else
    {
     Hurt();
    }
   }
  }
 }
}

Enemy.cs
/*/
* Script by Devin Curry
* www.Devination.com
* www.youtube.com/user/curryboy001
* Please like and subscribe if you found my tutorials helpful :D
* Twitter: https://twitter.com/Devination3D
/*/
using UnityEngine;
using System.Collections;

public class Enemy : MonoBehaviour
{
 public LayerMask enemyMask;
 public float speed = 1;
 Rigidbody2D myBody;
 Transform myTrans;
 float myWidth;

 void Start ()
 {
  myTrans = this.transform;
  myBody = this.GetComponent();
  myWidth = this.GetComponent().bounds.extents.x;
 }
 
 void FixedUpdate ()
 {
  //Check to see if there's ground in front of us before moving forward
  Vector2 lineCastPos = myTrans.position - myTrans.right * myWidth;
  Debug.DrawLine(lineCastPos, lineCastPos + Vector2.down);
  bool isGrounded = Physics2D.Linecast(lineCastPos, lineCastPos + Vector2.down, enemyMask);

  //If theres no ground, turn around
  if(!isGrounded)
  {
   Vector3 currRot = myTrans.eulerAngles;
   currRot.y += 180;
   myTrans.eulerAngles = currRot;
  }

  //Always move forward
  Vector2 myVel = myBody.velocity;
  myVel.x = -myTrans.right.x * speed;
  myBody.velocity = myVel;
 }

 public void Hurt()
 {
  Destroy(this.gameObject);
 }
}

Tuesday, April 19, 2016

VerletKha: Collision Primitives

Summary

A demonstration of particle vs primitive circle collision detection. All objects are draggable (you'll need to click in the very center of the collision circles).

Source Code

View on GitHub