基本信息
源码名称:unity2d酷跑游戏(简易实现功能,慎重下载)
源码大小:49.48M
文件格式:.zip
开发语言:C#
更新时间:2016-11-28
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
using UnityEngine;
using System.Collections;
using System;
public class PlayerStateController : MonoBehaviour
{
public float delaytime; //按键延迟时间
public static int jumpcount = 0; //三级跳级数
public Vector2 jumpforce; //精灵跳跃力
public Vector2 slideforce; //精灵下滑的力
public Rigidbody2D playerBody = null;
public BoxCollider2D playerCollider = null;
//定义游戏人物的各种状态
public enum playerStates
{
run = 0,
jump,
slide,
falling,
landing,
}
//定义委托和事件
public delegate void playerStateHandler(PlayerStateController.playerStates newState);
public static event playerStateHandler onStateChange;
void Start()
{
playerBody = GetComponent<Rigidbody2D>();
playerCollider = GetComponent<BoxCollider2D>();
}
void Update()
{
if (Input.GetKeyDown(KeyCode.S)&&!Input.GetKeyDown(KeyCode.L))
{
playerCollider.size = new Vector2(0.6232159f, 0.4215094f);
playerCollider.offset = new Vector2(0.1357977f, -0.2985263f);
if (onStateChange != null)
{
onStateChange(PlayerStateController.playerStates.slide);
}
}
if (Input.GetKey(KeyCode.S))
{
playerBody.AddForce(slideforce);
onStateChange(PlayerStateController.playerStates.slide);
}
if (Input.GetKeyUp(KeyCode.S))
{
playerCollider.size = new Vector2(0.6232159f, 1.004287f);
playerCollider.offset = new Vector2(0.1357977f, -0.007138014f);
}
if (Input.GetKeyDown(KeyCode.L) && !Input.GetKey(KeyCode.S))
{
if (onStateChange != null)
if (GetComponent<Animator>().GetBool("falling") == true || jumpcount < 3)
{
onStateChange(PlayerStateController.playerStates.jump);
playerBody.AddForce(jumpforce);
jumpcount ;
}
}
}
void LateUpdate()
{
if (Input.GetKeyUp(KeyCode.S))
{
if (!GetComponent<Animator>().GetBool("falling"))
{
onStateChange(PlayerStateController.playerStates.run);
}
}
if (Input.GetKeyUp(KeyCode.L))
{
if (!GetComponent<Animator>().GetBool("falling"))
{
// onStateChange(PlayerStateController.playerStates.run);
}
}
}
}