这是控制移动的代码,还没做完
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMove : View {
#region 常量
const float grivaty = 9.8f; //重力
const float m_jumpValue = 5f;
const float m_moveSpeed = 13f; //左右横跳速度
const float m_SpeedAddDis = 200; //每隔多少米加速度
const float m_SpeedAddRate = 0.5f; //加速度(关卡越高,速度越快)
const float m_MaxSpeed = 40f; //最大速度
#endregion
#region 事件
#endregion
#region 字段
public float speed = 20; //原始速度
CharacterController m_cc;
InputDirection m_inputDir = InputDirection.NULL;
bool activeInput = false;
/// <summary>
/// 起始位置
/// </summary>
Vector3 m_mousePos;
int m_nowIndex;
public int m_targetIndex = 1;
float m_xDistance; //左右横跳距离
float m_yDistance; //上跳高度
bool m_isSlide = false; //是否在滑动状态
float m_slideTime; //滑动时间
float m_SpeedAddCount; //距离下次加速度前累积走了多少米
GameModel gm;
//被撞后的减速再加速过程
//记录速度
float m_MaskSpeed;
//增加速度的速率
float m_AddRate = 10f;
//是否撞击
bool m_IsHit = false;
#endregion
#region 属性
public override string Name{ get{ return Const.V_PlayerMove;}}
public float Speed{
get{ return speed;}
set{
speed = value;
if (speed > m_MaxSpeed) {
speed = m_MaxSpeed;
}
}
}
#endregion
#region 方法
#region 移动
IEnumerator UpdateAction(){
while (true) {
if (!gm.IsPause && gm.IsPlay) {
m_yDistance -= grivaty * Time.deltaTime;//模拟下落重力
m_cc.Move ((transform.forward * speed + new Vector3(0,m_yDistance,0)) * Time.deltaTime);
MoveControl ();
UpdatePosition ();
UpdateSpeed ();
}
yield return 0;
}
}
/// <summary>
/// 获取输入
/// </summary>
void GetInputDirection(){
m_inputDir = InputDirection.NULL;
//手势识别
//初始按下,记录按下时位置
if(Input.GetMouseButtonDown(0)){
activeInput = true;
m_mousePos = Input.mousePosition;//鼠标在屏幕上的位置
}
//滑动
if (Input.GetMouseButton (0) && activeInput) {
//滑动后位置-起始位置 = 偏移向量
Vector3 Dir = Input.mousePosition - m_mousePos;
if(Dir.magnitude > 20){
//手势向右或右上方向
if(Mathf.Abs(Dir.x) > Mathf.Abs(Dir.y) && Dir.x > 0){
m_inputDir = InputDirection.Right;
}else if(Mathf.Abs(Dir.x) > Mathf.Abs(Dir.y) && Dir.x < 0){//手势向左或左上方向
m_inputDir = InputDirection.Left;
}else if(Mathf.Abs(Dir.x) < Mathf.Abs(Dir.y) && Dir.y > 0){//手势向上
m_inputDir = InputDirection.Up;
}else if(Mathf.Abs(Dir.x) < Mathf.Abs(Dir.y) && Dir.y < 0){//手势向下
m_inputDir = InputDirection.Down;
}
activeInput = false;
}
}
//键盘识别
if (Input.GetKeyDown (KeyCode.W) || Input.GetKeyDown (KeyCode.Space)) {
m_inputDir = InputDirection.Up;
} else if (Input.GetKeyDown (KeyCode.S)) {
m_inputDir = InputDirection.Down;
} else if (Input.GetKeyDown (KeyCode.A)) {
m_inputDir = InputDirection.Left;
} else if (Input.GetKeyDown (KeyCode.D)) {
m_inputDir = InputDirection.Right;
}
print (m_inputDir);
}
//更新位置
void UpdatePosition(){
GetInputDirection ();
switch (m_inputDir) {
case InputDirection.NULL:
break;
case InputDirection.Right: //跳向右边
if (m_targetIndex < 2) {
Game.Instance.sound.PlayEffectBG ("Se_UI_Huadong");
m_targetIndex++;
m_xDistance = 2;
SendMessage ("AnimManager", m_inputDir);
}
break;
case InputDirection.Left: //跳向左边
if (m_targetIndex > 0) {
Game.Instance.sound.PlayEffectBG ("Se_UI_Huadong");
m_targetIndex--;
m_xDistance = -2;
SendMessage ("AnimManager", m_inputDir);
}
break;
case InputDirection.Down:
if (m_isSlide == false) {
Game.Instance.sound.PlayEffectBG ("Se_UI_Slide");
m_isSlide = true;
m_slideTime = 0.733f;
SendMessage ("AnimManager", m_inputDir);
}
break;
case InputDirection.Up:
if (m_cc.isGrounded) {
Game.Instance.sound.PlayEffectBG ("Se_UI_Jump");
m_yDistance = m_jumpValue;
SendMessage ("AnimManager", m_inputDir);
}
break;
default:
break;
}
}//UpdatePosition_end
//移动
void MoveControl(){
//左右移动
if (m_targetIndex != m_nowIndex) {
float move = Mathf.Lerp (0, m_xDistance, Time.deltaTime * m_moveSpeed);
transform.position += new Vector3 (move, 0, 0);
m_xDistance -= move;//插值永远不会等于最终值
if(Mathf.Abs(m_xDistance) < 0.05){ //往左为负值
m_xDistance = 0;
m_nowIndex = m_targetIndex;
switch(m_nowIndex)
{
case 0:
transform.position = new Vector3 (-2, transform.position.y, transform.position.z);
break;
case 1:
transform.position = new Vector3 (0, transform.position.y, transform.position.z);
break;
case 2:
transform.position = new Vector3 (2, transform.position.y, transform.position.z);
break;
}
}
}//if_end
if (m_isSlide) {
m_slideTime -= Time.deltaTime;
if (m_slideTime < 0) {
m_isSlide = false;
m_slideTime = 0;
}
}
}
//更新速度
void UpdateSpeed(){
m_SpeedAddCount += speed * Time.deltaTime;
if (m_SpeedAddCount > m_SpeedAddDis) {
m_SpeedAddCount = 0;
speed += m_SpeedAddRate;
}
}
#endregion
//减速
public void HitObstacles(){
if (m_IsHit)
return;
m_IsHit = true;
m_MaskSpeed = speed;
Speed = 0;
StartCoroutine (DecreaseSpeed ());
}
IEnumerator DecreaseSpeed(){
while(speed <= m_MaskSpeed){
Speed += Time.deltaTime * m_AddRate;
yield return 0;
}
m_IsHit = true;
}
#endregion
#region Unity回调
private void OnTriggerEnter(Collider other){
if(other.gameObject.tag == Tag.smallFence){
other.gameObject.SendMessage ("HitPlayer",transform.position);
HitObstacles ();
Game.Instance.sound.PlayEffectBG("Se_UI_Hit");
}else if(other.gameObject.tag == Tag.bigFence){
if(m_isSlide)
return;
other.gameObject.SendMessage ("HitPlayer",transform.position);
Game.Instance.sound.PlayEffectBG("Se_UI_Hit");
HitObstacles ();
}else if(other.gameObject.tag == Tag.block){//撞到集装箱
Game.Instance.sound.PlayEffectBG("Se_UI_End");
other.gameObject.SendMessage ("HitPlayer",transform.position);
//sendEvent 游戏结束
SendEvent(Const.E_EndGame);
}else if(other.gameObject.tag == Tag.smallBlock){//撞到集装箱侧面
Game.Instance.sound.PlayEffectBG("Se_UI_End");
other.transform.parent.parent.gameObject.SendMessage ("HitPlayer",transform.position);
//sendEvent 游戏结束
SendEvent(Const.E_EndGame);
}else if(other.gameObject.tag == Tag.beforTrigger){//撞到集装箱侧面
other.transform.parent.parent.gameObject.SendMessage ("HitTrigger",transform.position);
//sendEvent 游戏结束
SendEvent(Const.E_EndGame);
}
}
private void Awake(){
m_cc = GetComponent<CharacterController> ();
gm = GetModel<GameModel> ();
}
private void Start(){
StartCoroutine (UpdateAction ());
}
void Update(){
//测试是否暂停
// if (Input.GetKeyDown (KeyCode.V)) {
// gm.IsPause = true;
// Game.Instance.objectPool.Spawn("Block_People",pos);
// }
// if (Input.GetKeyDown (KeyCode.M)) {
// gm.IsPause = false;
// }
}
#endregion
#region 事件回调
public override void HandleEvent (string name, object data)
{
}
#endregion
#region 帮助方法
#endregion
}
这是player的所有设置
之前以为是coilider的问题,但是把coilider抬高了一样掉到地下去