// Update is called once per frame void Update() { if (((Vector2)(originPos - CharacterControl.instance._collider.bounds.center)).sqrMagnitude < Mathf.Pow(nearRange, 2) && !Rest) //是否在靠近的范围内 { transform.position = Vector2.MoveTowards(transform.position, CharacterControl.instance._collider.bounds.center, maxDistanceDelta); } else { transform.position = Vector2.MoveTowards(transform.position, new Vector2(scale * Mathf.Sin(speed * Time.time + randomTime1) + originPos.x, scale * Mathf.Sin(speed * Time.time + randomTime2) + originPos.y), maxDistanceDelta * 0.6f); } if (isCharacterIn) { if (!Rest) { if (MyInput.instance.isGetJumpDown()) { CharacterControl.instance.isPlatJump = true; Rest = true; particle.gameObject.SetActive(true); Invoke("reset", restTime); SR.color = disableColor; _light.range = light_rest_range; } } } transform.position = GameFunction.getVector3(transform.position.x, transform.position.y, originPos.z); //纠正Z值 }
//修正跳跃产生的形变 void correctJumpScale() { if (currentState != state.jump && lastState == state.jump) { transform.localScale = Dir == dir.right ? originScale : GameFunction.getVector3(-originScale.x, originScale.y, originScale.z); } }
IEnumerator IE_unlock() { Transform trans_shadow = GameFunction.GetGameObjectInChildrenByName(this.gameObject, "Shadow").transform; Vector3 originScale_shadow = trans_shadow.localScale; const float duration = 1.8f; float timer = 0; while (timer < duration) { timer += Time.deltaTime; float t = timer / duration; trans_shadow.localScale = Vector3.Lerp(originScale_shadow, GameFunction.getVector3(1, 0, 1), t); SR_Stone_crack.color = Color.Lerp(SR_Stone_crack.color, Color.black, t); yield return(null); } Stone_trans.GetComponent <Rigidbody2D>().isKinematic = false; this.enabled = false; this.GetComponent <BoxCollider2D>().enabled = false; //Destroy(this.gameObject); }
private void FixedUpdate() { //夜晚不活动 if (!isNight) { Line.transform.localScale = Mathf.Sin(Time.time / cycle) * scale * GameFunction.getVector3(0, originScale.y, 0) + originScale; Spider.transform.position = SR_Line.bounds.min + GameFunction.getVector3(0, 0, 0.001f); } }
//修正冲刺产生的形变 void correntDashScale() { if (lastState == state.dash) { if (currentState != state.dash) { transform.localScale = Dir == dir.right?originScale:GameFunction.getVector3(-originScale.x, originScale.y, originScale.z); } } }
private void FixedUpdate() //Update 背景会抖动 { // 背景z轴不可超过10 Vector3 tVector = nowPosition - this.transform.position; tVector.z = 0; for (int i = 0; i < maps.Length; i++) { maps[i].transform.localPosition = GameFunction.getVector3((maps[i].transform.localPosition - tVector * (maps[i].transform.localPosition.z * 0.1f)).x, (maps[i].transform.localPosition - 0.2f * tVector * (maps[i].transform.localPosition.z * 0.1f)).y, maps[i].transform.localPosition.z); } nowPosition = this.transform.position; }
Vector3 getRayPos(Vector2 startPos, float angle) { int mask = 1 << LayerMask.NameToLayer("terrain"); Vector2 dir = new Vector2(Mathf.Cos(angle * Mathf.Deg2Rad), Mathf.Sin(angle * Mathf.Deg2Rad)); RaycastHit2D hitPoint = Physics2D.Raycast(startPos, dir, 9999, mask); if (hitPoint.transform.Equals(null)) { return(startPos + dir * 999); } return(GameFunction.getVector3(hitPoint.point.x, hitPoint.point.y, point[0].position.z)); }
public override void onStart() { base.onStart(); changeState(currentState); changeDir(dir.left); eye_Left = colliderID[0].bounds.min - transform.position + GameFunction.getVector3(-0.2f, 0.2f, 0); changeDir(dir.right); eye_Right = colliderID[0].bounds.max + Vector3.down * colliderID[0].bounds.size.y - transform.position + GameFunction.getVector3(0.2f, 0.2f, 0); Attack_Effect = GameFunction.GetGameObjectInChildrenByName(this.gameObject, "Attack_Effect"); }
void getElements() { TriggerElement.Clear(); int mask = 1 << 16; Physics2D.BoxCastNonAlloc(BoxColl.bounds.center, GameFunction.getVector3(BoxColl.size.x * Mathf.Abs(transform.lossyScale.x), BoxColl.size.y * Mathf.Abs(transform.lossyScale.y), 1), transform.rotation.eulerAngles.z, Vector2.zero, hitPoints, 0, mask); for (int i = 0; hitPoints[i].transform != null; i++) { if (hitPoints[i].collider != BoxColl) //排除自己 { TriggerElement.Add(hitPoints[i].collider.GetComponent <Element>().element); } } }
private void Awake() { System.DateTime currentTime = new System.DateTime(); Random.InitState(currentTime.Millisecond); script1 = GameObject.Find("MainCamera").GetComponent <CameraFollow>(); script2 = GameObject.Find("MainCamera").GetComponent <CameraFollow_Start>(); instance = this; character = GameObject.Find("character"); character.transform.position = GameFunction.getVector3(BornPosition[TheSceneManager.getInstance().BornPositionNum].position.x, BornPosition[TheSceneManager.getInstance().BornPositionNum].position.y, -0.1f); // 更改位置 character.GetComponent <CharacterControl>().enabled = false; black = GameObject.Find("black"); script1.enabled = false; }
private void Update() //Update 背景会抖动 { if (test.isEditor) //编辑模式下运行该函数 { try { // 背景z轴不可超过10 Vector3 tVector = nowPosition - this.transform.position; tVector.z = 0; for (int i = 0; i < maps.Length; i++) { maps[i].transform.localPosition = GameFunction.getVector3((maps[i].transform.localPosition - tVector * (maps[i].transform.localPosition.z * 0.1f)).x, (maps[i].transform.localPosition - 0.2f * tVector * (maps[i].transform.localPosition.z * 0.1f)).y, maps[i].transform.localPosition.z); } for (int i = 0; i < background.Length; i++) { background[i].transform.position = background[i].transform.position - tVector * (background[i].transform.position.z * 0.1f); } nowPosition = this.transform.position; } catch { maps = GameObject.FindGameObjectsWithTag("map"); background = GameObject.FindGameObjectsWithTag("map_background"); nowPosition = this.transform.position; } } //时间暂停时执行 if (Time.timeScale < 0.1f) { Vector3 tVector = nowPosition - this.transform.position; tVector.z = 0; for (int i = 0; i < maps.Length; i++) { maps[i].transform.localPosition = GameFunction.getVector3((maps[i].transform.localPosition - tVector * (maps[i].transform.localPosition.z * 0.1f)).x, (maps[i].transform.localPosition - 0.2f * tVector * (maps[i].transform.localPosition.z * 0.1f)).y, maps[i].transform.localPosition.z); } nowPosition = this.transform.position; } }
private void Update() { isJump = !OnGround(); //测试 isInRain = checkIsInRain(); //是否在雨中 if (isHorizontalBounce) { rig.velocity = new Vector2(rig.velocity.x, rig.velocity.y); } else { rig.velocity = new Vector2(0, rig.velocity.y); } switch (currentState) { case state.normal: changeDir(); // 行走 if ((isDoubleKeyDown() != 0 || MyInput.instance.isGetRun()) && isEnable[(int)state.run] && !CharacterAttribute.GetInstance().isOverLoad_breath) { currentState = state.run; //双击变为奔跑 } if (MyInput.instance.isGetJumpDown() && isEnable[(int)state.jump] && isGetInput) { currentState = state.jump; //跳跃 XJumpSpeed = Walkspeed + 2.5f; } attack(); // 攻击 shoot(); //射击 Throw(); //扔 dash(); //冲刺 if (OnGround() == false) { currentState = state.fall; //落下 XJumpSpeed = Walkspeed + 2.5f; YJumpSpeed = 0; } break; case state.walk: currentState = state.normal; //闲置 changeDir(); if ((isDoubleKeyDown() != 0 || MyInput.instance.isGetRun()) && isEnable[(int)state.run] && !CharacterAttribute.GetInstance().isOverLoad_breath) { currentState = state.run; //奔跑 } if (MyInput.instance.isGetJumpDown() && isEnable[(int)state.jump] && isGetInput) { currentState = state.jump; //跳跃 XJumpSpeed = Walkspeed + 2.5f; } dash(); //冲刺 Throw(); //扔 attack(); // 攻击 shoot(); //射击 if (OnGround() == false) { currentState = state.fall; //落下 XJumpSpeed = Walkspeed + 2.5f; YJumpSpeed = 0; } break; case state.run: if ((CharacterAttribute.GetInstance().isOverLoad_breath)) { currentState = state.normal; break; //如果气息不够则退回站立状态 } CharacterAttribute.GetInstance().Breath -= CharacterAttribute.GetInstance().expend_run *Time.deltaTime; //奔跑气息消耗 if (MyInput.instance.isGetLeft() && isGetInput) { rig.velocity += new Vector2(-RunSpeed, 0); Dir = dir.left; } if (MyInput.instance.isGetRight() && isGetInput) { rig.velocity += new Vector2(RunSpeed, 0); Dir = dir.right; } if (MyInput.instance.isGetLeftUp() || MyInput.instance.isGetRightUp() && isGetInput) { currentState = state.normal; //闲置 } attack(); // 攻击 shoot(); //射击 Throw(); //扔 dash(); //冲刺 if (MyInput.instance.isGetJumpDown() && isEnable[(int)state.jump] && isGetInput) { currentState = state.jump; //跳跃 XJumpSpeed = RunSpeed + 1; } if (OnGround() == false) { currentState = state.fall; //落下 XJumpSpeed = RunSpeed + 1; YJumpSpeed = 0; } break; case state.jump: if (JumpAccelerateTime < 0.01f) { _Timer_JumpScale = 0; } isDoubleKeyDown(); //计时 currentState = state.fall; //下落 if (isPlatJump) //连跳 { isPlatJump = false; JumpAccelerateTime = 0; YJumpSpeed = JumpSpeed; rig.velocity = Vector2.zero; _jumpTimes++; canJump = false; Time_pressJumpKey = 0; } //是否在跳跃(按键) if (MyInput.instance.isGetJump() && isGetInput) { Time_pressJumpKey += Time.deltaTime; canJump = true; } else { canJump = false; } if (Time_pressJumpKey > MaxJumpTime * (XJumpSpeed < RunSpeed ? 0.92f : 1.0f) * 0.6f) //剩余50%时间自动跳跃 { canJump = true; } if (JumpAccelerateTime < MaxJumpTime * (XJumpSpeed < RunSpeed ? 0.92f : 1.0f) && !CharacterAttribute.GetInstance().isOverLoad_breath&& canJump) { CharacterAttribute.GetInstance().Breath -= CharacterAttribute.GetInstance().expend_jump *Time.deltaTime; //气息消耗 JumpAccelerateTime += (Time.deltaTime * jumpTimeFactor); //YJumpSpeed = YJumpSpeed - Mathf.Pow(Yacceleration * Time.deltaTime,3); YJumpSpeed = ((-JumpSpeed / Mathf.Pow(MaxJumpTime * (XJumpSpeed < RunSpeed ? 0.92f : 1.0f), 2)) * Mathf.Pow(JumpAccelerateTime, 2) + JumpSpeed); if (YJumpSpeed < 0) { YJumpSpeed = 0; } rig.velocity = GameFunction.getVector2(rig.velocity.x, YJumpSpeed); currentState = state.jump; } JumpMove(); //跳跃时移动 //跳跃形变 _Timer_JumpScale += (Time.deltaTime * jumpTimeFactor); float t_jumpScale = _Timer_JumpScale / (jumpScale_Time); transform.localScale = Vector3.Lerp(Dir == dir.right?jumpScale:GameFunction.getVector3(-jumpScale.x, jumpScale.y, jumpScale.z), Dir == dir.right?originScale:GameFunction.getVector3(-originScale.x, originScale.y, originScale.z), t_jumpScale); if (attack()) //攻击 { rig.velocity = GameFunction.getVector2(rig.velocity.x, 0); YJumpSpeed = 0; } shoot(); //射击 if (Throw()) //扔 { rig.velocity = GameFunction.getVector2(rig.velocity.x, 0); YJumpSpeed = 0; } if (dash()) //冲刺 { dashTimes = 1; } if (AtTop()) //判断是否顶到墙 { currentState = state.fall; YJumpSpeed = 0; } if (currentState == state.fall) //使动作更加自然 { YJumpSpeed = 0; } break; case state.dash: isDoubleKeyDown(); //计时 if (attack()) //攻击 { YJumpSpeed = 0; _dashTime = 0; } if (shoot()) //射击 { YJumpSpeed = 0; _dashTime = 0; } _dashTime += Time.deltaTime; Timer_dashScale += Time.deltaTime; float t_dashScale = Timer_dashScale / (proportion_dashScale * DashTime); transform.localScale = Vector3.Lerp(Dir == dir.right ? dashScale : GameFunction.getVector3(-dashScale.x, dashScale.y, dashScale.z), Dir == dir.right ? originScale : GameFunction.getVector3(-originScale.x, originScale.y, originScale.z), t_dashScale); rig.velocity = new Vector2(Dir == dir.left ? -DashSpeed : DashSpeed, 0); if (_dashTime > DashTime) { currentState = isJump ? state.fall : state.endDash; YJumpSpeed = 0; _dashTime = 0; //完全冲刺后产生惯性 if (isJump) { StartCoroutine(IE_dashInertia()); } else { _Timer_dash = 0; } } break; case state.attack1: // 空中攻击移动----------------- if (isJump) { //JumpMove(); YJumpSpeed = YJumpSpeed + Yacceleration * Time.deltaTime; if (YJumpSpeed > JumpSpeed) { YJumpSpeed = JumpSpeed; } rig.velocity = new Vector2(rig.velocity.x, -YJumpSpeed); } //--------------------------------- if (!isPlayAnimation()) { if (isJump) { currentState = state.fall; } else { currentState = state.normal; } } break; case state.attack2: // 空中攻击移动----------------- if (isJump) { //JumpMove(); YJumpSpeed = YJumpSpeed + Yacceleration * Time.deltaTime; if (YJumpSpeed > JumpSpeed) { YJumpSpeed = JumpSpeed; } rig.velocity = new Vector2(rig.velocity.x, -YJumpSpeed); } //--------------------------------- if (!isPlayAnimation()) { if (isJump) { currentState = state.fall; } else { currentState = state.normal; } } break; case state.jumpshoot: if (isJumpShootBack) { _time3 += Time.deltaTime; //后退效果 if (_time3 < jumpshoot_backTime) { rig.velocity = new Vector2(Dir == dir.left ? 20 : -20, 18); } else { rig.velocity = Vector2.zero; } if (!isPlayAnimation()) { isJumpShootBack = false; _time3 = 0; currentState = state.fall; } } break; case state.shoot: if (!isPlayAnimation()) { currentState = state.normal; } break; case state.hurt: int t = 20; _hurtTime += Time.deltaTime; rig.velocity = new Vector2((Dir == dir.left ? (-t / HurtTime * _hurtTime + t) : -(-t / HurtTime * _hurtTime + t)), 0); YJumpSpeed = YJumpSpeed + Yacceleration * Time.deltaTime; //为Y轴添加重力 if (YJumpSpeed > JumpSpeed) { YJumpSpeed = JumpSpeed; } rig.velocity = new Vector2(rig.velocity.x, -YJumpSpeed); if (_hurtTime > HurtTime) { currentState = isJump == true ? state.fall : state.normal; _hurtTime = 0; } break; case state.fall: isDoubleKeyDown(); //计时 if (!isHorizontalBounce) { JumpMove(); //水平弹飞中不能移动 } YJumpSpeed = YJumpSpeed + Yacceleration * Time.deltaTime * jumpTimeFactor; if (YJumpSpeed > JumpSpeed) { YJumpSpeed = JumpSpeed; } if (!isBounce) { rig.velocity = GameFunction.getVector2(rig.velocity.x, -YJumpSpeed); } if (isBounce) { AtTop(); } if (MyInput.instance.isGetAttack() && isEnable[(int)state.attack1] && !CharacterAttribute.GetInstance().isOverLoad_breath&& isGetInput) //攻击 { CharacterAttribute.GetInstance().Breath -= CharacterAttribute.GetInstance().expend_attack; currentState = state.attack1; //攻击 CharacterObjectManager.instance.attack1(Dir); } if ((MyInput.instance.isGetJumpDown() && _jumpTimes < JumpTimes && isEnable[(int)state.jump] && isGetInput) || isPlatJump) //连跳 { isPlatJump = false; currentState = state.jump; JumpAccelerateTime = 0; YJumpSpeed = JumpSpeed; rig.velocity = Vector2.zero; _jumpTimes++; canJump = false; Time_pressJumpKey = 0; } shoot(); //射击 Throw(); //扔 if (dash()) //冲刺 { dashTimes = 1; } if (OnGround()) // 判断是否落地 { currentState = state.normal; } break; case state.endDash: //惯性 _Timer_dash += Time.deltaTime; float t_endDash = _Timer_dash / DashInertiaTime; rig.velocity = Vector2.Lerp((Dir == dir.left ? Vector2.left : Vector2.right) * DashSpeed, Vector2.zero, t_endDash); if (_Timer_dash > DashInertiaTime) { currentState = state.normal; //播放粒子特效 if (!isJump) { CharacterObjectManager.instance.PlayFallParticle(isInRain); } } break; case state.Throw: // 空中移动----------------- if (isJump) { YJumpSpeed = YJumpSpeed + Yacceleration * Time.deltaTime; if (YJumpSpeed > JumpSpeed) { YJumpSpeed = JumpSpeed; } rig.velocity = new Vector2(rig.velocity.x, -YJumpSpeed); } //--------------------------------- if (!isPlayAnimation()) { currentState = isJump? state.fall:state.normal; } break; } if (isAttack) // 为双击计时 { _DoubleAttackSpaceTime += Time.deltaTime; if (_DoubleAttackSpaceTime > DoubleAttackSpaceTime) { isAttack = false; _DoubleAttackSpaceTime = 0; } } if (isHurt) //受伤 { flash(); // 人物闪烁 } rig.velocity += additionalVelocity; //计算额外的速度 additionalVelocity = Vector2.zero; if (lastDir != Dir) // 翻转图片 判断减少运算量 { //SpriteRenderer.flipX = SpriteRenderer.flipX == false ? true : false; GameFunction.t_Vector3 = transform.localScale; GameFunction.t_Vector3.x *= -1; transform.localScale = GameFunction.t_Vector3; } lastDir = Dir; changeAnimation(); //改变当前动画 CharacterObjectManager.instance.changeDustParticle(currentState, lastState, isJump, isInRain, Dir); //改变尘土粒子效果 correctJumpScale(); //修正跳跃形变 correntDashScale(); //修正冲刺形变 lastState = currentState; }
protected override void _FixedUpdate() { base._FixedUpdate(); _isSeePlayer = isSeePlayer(); switch (currentState) { case Bat_state.stay: if (_isSeePlayer) { changeState(Bat_state.fly); changeDir(getPlayerDir()); } break; case Bat_state.fly: Timer_stay += Time.deltaTime; rig.velocity = GameFunction.getVector3(Mathf.Sin(Time.time / flyCycle), Mathf.Sin(0.6f * Time.time / flyCycle + random), 0) * flySpeed; float distanceFromCiling = DistanceFromCeiling(); if (distanceFromCiling > 1) { rig.velocity += Vector2.up * flySpeed * 2; } if (_isSeePlayer) { Timer_fly += Time.deltaTime; } else { if (distanceFromCiling < 1 && Timer_stay > Time_stay) { changeState(Bat_state.stay); } } if (Timer_fly > waitTime_fly) { dashDir = (CharacterControl.instance.getCollCenter() - transform.position).normalized; changeState(Bat_state.dash); transform.Rotate(GameFunction.getVector3(0, 0, getAngle(dashDir))); } changeDir(getPlayerDir()); break; case Bat_state.dash: Timer_dash += Time.deltaTime; rig.velocity = dashDir * dashSpeed; if (isNearWall(dashDir) == true || Timer_dash > MaxFlyTime) { changeState(Bat_state.fly); rig.velocity = Vector2.zero; Timer_fly = 0; transform.rotation = Quaternion.Euler(Vector3.zero); Timer_dash = 0; TR.enabled = false; } break; } }