public void BeFaned(Vector2 force) { Vector2 velocity = force / m_Rigidbody2D.mass * Time.fixedDeltaTime; if (m_PhysicsUpdate.IsContainVelocity("ContinuedAddForceNotGroundClear")) { m_PhysicsUpdate.AddVelocity("ContinuedAddForceNotGroundClear", velocity); } else { m_PhysicsUpdate.RegisterVelocity("ContinuedAddForceNotGroundClear", new VelocityAttribute(false, true, false, false, 0, false)); Vector2 ground = Vector2.zero; if (m_PhysicsUpdate.IsContainVelocity("ContinuedAddForce")) { ground = m_PhysicsUpdate.GetVelocity("ContinuedAddForce"); m_PhysicsUpdate.CancelVelocity("ContinuedAddForce"); } m_PhysicsUpdate.AddVelocity("ContinuedAddForceNotGroundClear", ground + velocity); Debug.Log(ground); } }
public void BeDied() { if (m_Controller.isActiveAndEnabled) { //播放死亡动画 GetComponent <Animator>().SetTrigger("died"); m_PhysicsUpdate.IsDied = true; Vector2 gVelo = m_PhysicsUpdate.GetVelocity("Gravity"); m_PhysicsUpdate.ResetVelocity("Move"); m_Controller.enabled = false; if (!m_CanResurrect) {//真死 Destroy(gameObject, m_DiedTime); } else {//假死 Invoke("FeignDeath", m_DiedTime); } } }
void FixedUpdate() { RaycastHit2D[] hits; Collider2D[] colliders; m_Animator.SetBool("isGround", m_PhysicsUpdate.IsGround); //获取上一帧的移动速度 m_LastMoveVelocity = m_PhysicsUpdate.GetVelocity("Move").x; //移动键判断 if (Mathf.Abs(Vector2.SqrMagnitude(m_PhysicsUpdate.GetVelocity("Rebound"))) < m_CanMoveReboundVelocity * m_CanMoveReboundVelocity) { float horizontal = Input.GetAxis("Horizontal_" + m_InputId); //没继续按相同方向键并且在地上则移动速度置零 if ((horizontal == 0 || m_LastMoveVelocity / horizontal <= 0) && m_PhysicsUpdate.IsGround) { m_PhysicsUpdate.ResetVelocity("Move"); m_LastMoveVelocity = 0; } //移动 if (Mathf.Abs(horizontal) > 0.1f) { float moveRate = horizontal * m_MoveAddRate * Time.fixedDeltaTime; //超过最大速度则保持最大速度 if (moveRate + m_LastMoveVelocity > m_MoveRate) { moveRate = m_MoveRate - m_LastMoveVelocity; } else if (moveRate + m_LastMoveVelocity < -m_MoveRate) { moveRate = -m_MoveRate - m_LastMoveVelocity; } m_PhysicsUpdate.AddVelocity("Move", new Vector2(moveRate, 0)); } TurnRotation(horizontal); } if (!m_IsAttracting) //没在用磁铁吸 { //技能一炸弹 if (Input.GetButtonDown("Skill1_" + m_InputId)) { if (m_Boom) { if (m_Boom.CanBoom) { m_Boom.Boom(); m_LastBoomTime = Time.time; } } else if (Time.time > m_LastBoomTime + 0.1f) { m_BoomLaunchController.LaunchPos = new Vector2((IsLeft?-1:1) * Mathf.Abs(m_BoomLaunchController.LaunchPos.x), m_BoomLaunchController.LaunchPos.y); m_BoomLaunchController.LaunchDirection = IsLeft ? Vector2.left : Vector2.right; m_Boom = (BoomController)m_BoomLaunchController.Launch(); } } } else { // m_PhysicsUpdate.ResetVelocity("Move"); } //大跳 if (Input.GetButton("Jump_" + m_InputId)) { if (m_IsRetaining) { m_RetentionTimer += Time.fixedDeltaTime; if (m_RetentionTimer > m_JumpRetentionTime) { m_RetentionTimer = 0; m_IsRetaining = false; } else { m_PhysicsUpdate.AddVelocity("RetentionJump", new Vector2(0, m_JumpRetentionForce / m_Rigidbody2D.mass * Time.fixedDeltaTime)); } } } //跳跃键判断 if (Input.GetButtonDown("Jump_" + m_InputId)) { m_IsJump = true; } if (m_IsJump) { m_JumpTimer += Time.fixedDeltaTime; if (m_JumpTimer > 0.3f) { m_JumpTimer = 0; m_IsJump = false; } //反弹 if (transform.parent == null) { colliders = Physics2D.OverlapBoxAll((Vector2)transform.position + m_BoxCollider2D.offset, m_BoxCollider2D.size + new Vector2(m_SensitivityOfRebound, m_SensitivityOfRebound), 0, 1 << 9); foreach (var collider in colliders) { if (collider.gameObject != gameObject) { if (m_OtherPlayerController.transform.parent == null) { BeRebound((transform.position - collider.transform.position).normalized * m_ReboundVelocity); m_OtherPlayerController.BeRebound((-transform.position + collider.transform.position).normalized * m_ReboundVelocity); break; } } } } //跳跃 if (m_PhysicsUpdate.IsGround && m_IsJump) { //判断上方有没有东西 bool canJump = true; hits = Physics2D.BoxCastAll((Vector2)transform.position + m_BoxCollider2D.offset, m_BoxCollider2D.size, 0, new Vector2(0, 1), 0.1f, m_PhysicsUpdate.ColliderLayer); foreach (var hit in hits) { if (hit.collider.gameObject != gameObject) { canJump = false; break; } } if (canJump) { m_JumpTimer = 0; m_IsJump = false; m_PhysicsUpdate.AddVelocity("Jump", new Vector2(0, m_JumpForce / m_Rigidbody2D.mass * Time.fixedDeltaTime)); m_IsRetaining = true; } } } //放开跳跃键 if (Input.GetButtonUp("Jump_" + m_InputId)) { m_RetentionTimer = 0; m_IsRetaining = false; } //技能二磁铁 if (Input.GetButton("Skill2_" + m_InputId)) { float horizontal = Input.GetAxis("Horizontal_" + m_InputId); float vertical = Input.GetAxis("Vertical_" + m_InputId); // TurnRotation(horizontal); if (!m_IsAttracting) {//第一帧进入吸 m_IsAttracting = true; if (m_CurrentAttractDirection == Vector2.zero) { m_CurrentAttractDirection = IsLeft ? Vector2.left : Vector2.right; } if (Mathf.Abs(horizontal) > Mathf.Abs(vertical)) { if (horizontal > 0.1f) { m_CurrentAttractDirection = Vector2.right; } else if (horizontal < -0.1f) { m_CurrentAttractDirection = Vector2.left; } } else if (Mathf.Abs(horizontal) < Mathf.Abs(vertical)) { if (vertical > 0.1f) { m_CurrentAttractDirection = Vector2.up; } else if (vertical < -0.1f) { m_CurrentAttractDirection = Vector2.down; } } } foreach (var t in m_BeAttractedList) { t.CancelAttracted(-m_CurrentAttractDirection * m_AttractedVelocity); } m_BeAttractedList.Clear(); if (m_CurrentAttractDirection.x != 0) { colliders = Physics2D.OverlapBoxAll(new Vector2(m_CurrentAttractDirection.x * (m_BoxCollider2D.size.x + m_AttractedLength) / 2, 0) + (Vector2)transform.position, new Vector2(m_AttractedLength, m_BoxCollider2D.size.y * 0.75f), 0); foreach (var c in colliders) { if (c.gameObject != gameObject) { var beA = c.transform.GetComponent <BeAttractedComponent>(); if (beA != null && beA.isActiveAndEnabled) { beA.BeAttracted(-m_CurrentAttractDirection * m_AttractedVelocity); m_BeAttractedList.Add(beA); } } } } else if (m_CurrentAttractDirection.y != 0) { colliders = Physics2D.OverlapBoxAll(new Vector2(0, m_CurrentAttractDirection.y * (m_BoxCollider2D.size.y + m_AttractedLength) / 2) + (Vector2)transform.position, new Vector2(m_BoxCollider2D.size.x * 0.75f, m_AttractedLength), 0); foreach (var c in colliders) { if (c.gameObject != gameObject) { var beA = c.transform.GetComponent <BeAttractedComponent>(); if (beA != null && beA.isActiveAndEnabled) { m_BeAttractedList.Add(beA); beA.BeAttracted(-m_CurrentAttractDirection * m_AttractedVelocity); } } } } else { Debug.LogError("m_CurrentAttractDirection is wrong!"); } } else if (m_IsAttracting) {//第一帧退出吸 m_IsAttracting = false; foreach (var t in m_BeAttractedList) { t.CancelAttracted(-m_CurrentAttractDirection * m_AttractedVelocity); } m_BeAttractedList.Clear(); } //磁铁反作用力 if (m_BeAttractedList.Count > 0) { if (m_CurrentAttractDirection == Vector2.left || m_CurrentAttractDirection == Vector2.right) { m_PhysicsUpdate.AddVelocity("AttractX", m_CurrentAttractDirection * m_AttractedVelocity); } else { m_PhysicsUpdate.AddVelocity("AttractY", m_CurrentAttractDirection * m_AttractedVelocity); } } //上下吸时重新计算重力 if (m_BeAttractedList.Count != 0 && m_CurrentAttractDirection != Vector2.left && m_CurrentAttractDirection != Vector2.right) { m_PhysicsUpdate.ResetVelocity("Gravity"); m_PhysicsUpdate.AddVelocity("Gravity", new Vector2(0, m_AttractedGravityVelocity)); } }