Sample Code Dodger
usermovement.cs | |
File Size: | 1 kb |
File Type: | cs |
Player Class
using UnityEngine;
using System.Collections;
public class UserMovement : MonoBehaviour
{
public Animator m_Animator;
float m_fXSpike;
public float m_fHitpenalty =.5f;
int m_iPlayerIndex;
void Start ()
{
m_iPlayerIndex = 0;
m_Animator = GetComponent ();
}
// Update is called once per frame
void Update ()
{
if(Input.GetKeyDown(KeyCode.Space))
{
m_Animator.SetBool("IsJump", true);
}
if(Input.GetKeyDown(KeyCode.D))
{
m_Animator.SetBool("IsDash", true);
}
if(Input.GetMouseButtonDown(0))
{
Vector3 p = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Collider2D r = Physics2D.OverlapPoint(p);
if(r!= null)
{
if( r.gameObject != null && r.tag == "JumpButton" )
{
m_Animator.SetBool("IsJump", true);
}
if( r.gameObject != null && r.tag == "DashButton" )
{
m_Animator.SetBool("IsDash", true);
}
}
}
}
public void OnTriggerEnter2D(Collider2D other)
{
//if (other == null) {
// return ;
//}
if (other.tag == "Enemy")
{
ApplyHit ();
other.gameObject.SetActive (false);
}
if (other.tag == "Spike")
{
m_Animator.SetBool("IsDead",true); // Try and turn off the render
SoundManager.Get().PlayPlayerDeath();
}
else
{
//Landing
SoundManager.Get().PlayPlayerLand();
}
}
public void ApplyHit()
{
SoundManager.Get ().PlayPlayerHit();
m_Animator.SetBool("IsHit", true);
Vector3 pos = transform.parent.position;
pos .x -= m_fHitpenalty;
transform.parent.position = pos;
}
public void Hit()
{
m_Animator.SetBool("IsHit", false);
}
public void Running()
{
m_Animator.SetBool("IsJump", false);
}
public void Dash()
{
m_Animator.SetBool("IsDash", false);
}
///
/// Stop Rendering
///
public void Dead()
{
m_Animator.enabled = false;
GameLogic.Get().IsDead(m_iPlayerIndex);
}
}