/// <summary> /// 接触している Rigidbody を打ち上げる。 /// </summary> /// <param name="power">打ち上げる力</param> public void Launch(float power) { foreach (var c in m_touchingColliders) { Rigidbody2D rb = c?.GetComponent <Rigidbody2D>(); rb?.AddForce(Vector2.up * power, ForceMode2D.Impulse); } }
/// <summary> /// If it touches an object in the target layer, it damages it /// </summary> /// <param name="collision">Collision that occurred</param> private void OnCollisionStay2D(Collision2D collision) { if (attackCollisionMask.HasLayer(collision.gameObject.layer)) { if (collision.gameObject.GetComponent <IHealth>()?.Damage(big ? data.Attack : (ushort)(data.Attack * 0.5f)) ?? false) { Rigidbody2D body = collision.gameObject.GetComponent <Rigidbody2D>(); body?.AddForce(movement * (big ? data.Knockback : (data.Knockback * 0.75f))); } } }
private void OnCollisionEnter2D(Collision2D collision) { if (collision.gameObject.CompareTag("Player")) { CharacterHealth ch = collision.gameObject.GetComponent <CharacterHealth>(); Vector2 forcePush = (ch.transform.position - transform.position).normalized; Rigidbody2D targetRb2d = ch.GetComponent <Rigidbody2D>(); targetRb2d?.AddForce(forcePush * punchForce); rb2d?.AddForce(-forcePush * punchForce); ch.TakeDamage(damage); } }
/// <summary> /// Applies the spell's effect to the colliding object (usually damage) /// </summary> /// <param name="collision">Collision that occurred</param> protected virtual void Effect(Collider2D collision) { //play impact sound if (data.ImpactClip != null) { AudioManager.Instance.PlaySound(data.ImpactClip); } coll.enabled = false; //apply knockback Rigidbody2D body = collision?.gameObject.GetComponent <Rigidbody2D>(); body?.AddForce(transform.up * data.Knockback); Destroy(gameObject); // TODO: Animation? }
void Update() { /* flim and move if wall touch */ if (walltouch) { moveFunc(); } /*silde in wall */ if (walltouch && !isgrounded && rb.velocity.y < 0 && dead == false) { iamAttaking = false; if (Mathf.Abs(runspeedvalue) == runspeed / 2) { runspeedvalue = 0; } AnimState(4); rb.velocity = new Vector2(rb.velocity.x, slideVilocity); /*add effect */ if (timeElapsed > dustSpawnDelay) { var dust = Instantiate(dustPrefab); var pos = transform.position; pos.y += 1; dust.transform.position = pos; dust.transform.Rotate(0, 0, transform.localScale.x * 90); dust.transform.localScale = new Vector3(-transform.localScale.x, transform.localScale.y, transform.localScale.z); timeElapsed = 0; } timeElapsed += Time.deltaTime; } /*if grounded */ if (isgrounded == true && iamAttaking == false && dead == false) { extrajump = extrajumpsvalue; AnimState(1); timeElapsed = 0; } else if (isgrounded == false && !walltouch && iamAttaking == false && dead == false) { AnimState(2); } /*attack animation */ if (Input.GetKeyDown(KeyCode.Space) && iamAttaking == false && !walltouch && dead == false) { iamAttaking = true; Collider2D [] enemiestoDammage = Physics2D.OverlapBoxAll(attackPos.position, new Vector2(attackRangeX, attackRangeY), 0, whatIsenmies); for (int i = 0; i < enemiestoDammage.Length; i++) { /* enemiestoDammage[i].GetComponent<enemy>().takeDammage(1); */ enemiestoDammage[i].GetComponent <Collider2D>().enabled = false; /*push enmy */ Rigidbody2D rbeenmy = enemiestoDammage[i].GetComponent <Rigidbody2D>(); rbeenmy.isKinematic = true; rbeenmy.AddForce(transform.right * runspeedvalue * 1000); } rb.AddForce(transform.right * runspeedvalue * 120); AnimState(5); } }
private void Start() { rb2d.AddForce(transform.up * 500); }
//玩家移动 private void MoveUpdate() { if (m_lock) { return; } const int Wall = 1 << 8; bool left = Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A); bool right = Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D); bool up = Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.W); bool down = Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.S); if (Input.GetKeyUp(KeyCode.RightArrow) || Input.GetKeyUp(KeyCode.D) || Input.GetKeyUp(KeyCode.LeftArrow) || Input.GetKeyUp(KeyCode.A) || m_downCollider.IsTouchingLayers(Wall)) { m_canmove = true; } if (Mathf.Abs(m_rigidbody.velocity.x) > 0.01f) { if (m_rigidbody.velocity.x > 0) { m_actorState.SetState(ActorState.State.Right); } else { m_actorState.SetState(ActorState.State.Left); } } else { m_actorState.SetState(ActorState.State.Idle); } if (left && m_canmove) { m_actorState.SetState(ActorState.State.Left); //向左移动 m_rigidbody.AddForce(new Vector2(-ConstData.PlayerAcc, 0), ForceMode2D.Force); //最大速度 if (m_rigidbody.velocity.x < -ConstData.PlayerMaxVel) { m_rigidbody.velocity = new Vector2(-ConstData.PlayerMaxVel, m_rigidbody.velocity.y); } //爬墙 if (m_leftCollider.IsTouchingLayers(Wall) && !m_downCollider.IsTouchingLayers(Wall)) { m_actorState.SetState(ActorState.State.LeftWall); m_rigidbody.AddForce(new Vector2(0, ConstData.PlayerFri), ForceMode2D.Force); } } if (right && m_canmove) { //向右移动 m_rigidbody.AddForce(new Vector2(ConstData.PlayerAcc, 0), ForceMode2D.Force); //最大速度 if (m_rigidbody.velocity.x > ConstData.PlayerMaxVel) { m_rigidbody.velocity = new Vector2(ConstData.PlayerMaxVel, m_rigidbody.velocity.y); } //爬墙 if (m_rightCollider.IsTouchingLayers(Wall) && !m_downCollider.IsTouchingLayers(Wall)) { m_actorState.SetState(ActorState.State.RightWall); m_rigidbody.AddForce(new Vector2(0, ConstData.PlayerFri), ForceMode2D.Force); } } //跳跃 if (up) { //平地 if (m_downCollider.IsTouchingLayers(Wall)) { m_doubleJump = true; m_rigidbody.AddForce(new Vector2(0, ConstData.PlayerJump), ForceMode2D.Impulse); PlayAudio.Play(this.gameObject, "Sound/跳跃3"); } else //爬墙跳 if (m_rightCollider.IsTouchingLayers(Wall) && right) { PlayAudio.Play(this.gameObject, "Sound/跳跃2"); m_canmove = false; m_doubleJump = false; m_rigidbody.AddForce(new Vector2(-ConstData.PlayerRef, ConstData.PlayerJump * 1.3f), ForceMode2D.Impulse); } else if (m_leftCollider.IsTouchingLayers(Wall) && left) { PlayAudio.Play(this.gameObject, "Sound/跳跃2"); m_canmove = false; m_doubleJump = false; m_rigidbody.AddForce(new Vector2(ConstData.PlayerRef, ConstData.PlayerJump * 1.3f), ForceMode2D.Impulse); } else //双跳 if (m_doubleJump) { PlayAudio.Play(this.gameObject, "Sound/跳跃3"); m_doubleJump = false; m_rigidbody.velocity = new Vector2(m_rigidbody.velocity.x, 0); m_rigidbody.AddForce(new Vector2(0, ConstData.PlayerJump), ForceMode2D.Impulse); } } }
void Jump() { _rigidbody.AddForce(new Vector3(0, jumpPower, 0), ForceMode2D.Impulse); }
public void Jump() { rb.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse); }
// Update is called once per frame void AddForse() { rbody.isKinematic = false; rbody.AddForce(direction * powerForce, ForceMode2D.Impulse); }
// To move our enemy along the path // FixedUpdate is called a fixed number of time per frame. Ideal for working with physics void FixedUpdate() { // Checking if we have a path or not if (path7 == null) { return; } // Checking if our current waypoint is eqaul or greater than the total amount of waypoints along our path // If it is true then we haved reached the end of the path if (currentWaypoint7 >= path7.vectorPath.Count) { reachedEndOfPath7 = true; if (reachedEndOfPath7 == true) { Attack(); } return; } else { // There are more waypoint in the path. We haven't reached the end reachedEndOfPath7 = false; if (reachedEndOfPath7 == false) { StopAttack(); } } /* * return; * } * else * { * // There are more waypoint in the path. We haven't reached the end * reachedEndOfPath7 = false; * } */ // Getting the direction to the next waypoint along our path // path.vectorPath[currentWaypoint] gives the position of our current waypoint // rb.position is our current position // Gives us a vector from our position to our next waypoint // We are pointing and arrow from our current position to where we want to be which is our current waypoint and making sure the lenght of that arrow is 1 (normalized) Vector2 direction7 = ((Vector2)path7.vectorPath[currentWaypoint7] - rb7.position).normalized; // Getting a force we want to apply on our enemy to make it move in that direction. // Time.deltaTime is for making sure it doesn't vary depending on the frame rate Vector2 force7 = direction7 * speed7 * Time.deltaTime; // adding the force to our enemy rb7.AddForce(force7); // Finding the distance of our next waypoint. Which is between our current position and the next waypoint float distance7 = Vector2.Distance(rb7.position, path7.vectorPath[currentWaypoint7]); // Checking if we reached that current waypoint if (distance7 < nextwaypointDistance7) { // We want move to the next waypoint currentWaypoint7++; } // To flip our enemy towards the player // Checking if our current velocity of our enemy is less than some negative value means we are moving on the left if (rb7.velocity.x <= -0.01f) { transform.localScale = new Vector3(-1f, 1f, 1f); } // if it is positve then we are moving to the right else if (rb7.velocity.x >= 0.01f) { transform.localScale = new Vector3(1f, 1f, 1f); } animator.SetFloat("Speed", Mathf.Abs(rb7.velocity.x)); }
protected void DoubleTapCheck() { if (Player2 == false && Input.GetButtonDown("Right") || Player2 == true && Input.GetButtonDown("P2Right")) { if (RightButtonCooldown > 0 && RightButtonCount == 1) { if (this.transform.rotation.y == 0) { if (InputList[InputList.Count - 1] == "Forward") { rb2d.AddForce(new Vector2(transform.right.x * DashSpeed, 0)); InputList.Add("DashForward"); Debug.Log("DashForward"); } } else { if (InputList[InputList.Count - 1] == "Back") { rb2d.AddForce(new Vector2(-1 * (transform.right.x * DashSpeed), 0)); InputList.Add("DashBackward"); Debug.Log("DashBackward"); } } } else { RightButtonCooldown = 0.5f; RightButtonCount += 1; } } if (RightButtonCooldown > 0) { RightButtonCooldown -= 1 * Time.deltaTime; } else { RightButtonCount = 0; } if (Player2 == false && Input.GetButtonDown("Left") || Player2 == true && Input.GetButtonDown("P2Left")) { if (LeftButtonCooldown > 0 && LeftButtonCount == 1) { if (this.transform.rotation.y == 0) { if (InputList[InputList.Count - 1] == "Back") { rb2d.AddForce(new Vector2(-1 * (transform.right.x * DashSpeed), 0)); InputList.Add("DashBackward"); Debug.Log("DashBackward"); } } else { if (InputList[InputList.Count - 1] == "Forward") { rb2d.AddForce(new Vector2(transform.right.x * DashSpeed, 0)); InputList.Add("DashForward"); Debug.Log("DashForward"); } } } else { LeftButtonCooldown = 0.5f; LeftButtonCount += 1; } } if (LeftButtonCooldown > 0) { LeftButtonCooldown -= 1 * Time.deltaTime; } else { LeftButtonCount = 0; } }
public void OnCollision(Rigidbody2D CollidingObject, Vector3 BouncingNormalSurface) { CollidingObject.AddForce(-BouncingNormalSurface * BouncingForce); onBounce.Invoke(); }
public void Launch(Vector2 direction, float force) { rigidbody2d.AddForce(direction * force); }
public void TouchedJumpOrb() { myRigidbody2D.velocity = Vector2.zero; myRigidbody2D.AddForce(new Vector2(0, jumpSpeed), ForceMode2D.Impulse); airJumpCount = 0; }
public void Throw() { rb.AddForce(Vector3.right * throwForce); }
protected void applyImpulseForward() { Vector2 forward = direction(); rb.AddForce(forward * GetVelocity()); }
// Update is called once per frame void Update() { float h = Input.GetAxis("Horizontal"); //body.velocity = new Vector2(10 * h, body.velocity.y); if (body.position.x < 0.64) { body.AddForce(Vector2.right * h * speed); } //}// cria aceleração if (Input.GetKeyDown(KeyCode.O)) { body.transform.position = new Vector3(0, 0, 0); body.transform.rotation = Quaternion.EulerRotation(0, 0, 0); body.velocity = new Vector2(0, 0); aux = 0; } if (Input.GetKeyDown(KeyCode.Space)) { if (ispause) { Time.timeScale = 0; setapeso.transform.position = new Vector3(body.transform.position.x + 0.07f, body.transform.position.y - 1.65f, body.transform.position.z); setapeso.transform.rotation = Quaternion.EulerRotation(0, 0, 0); if (body.transform.position.x < 0.8f) { setanormal.transform.position = new Vector3(body.transform.position.x, body.transform.position.y + 1.48f, body.transform.position.z); setaatrito.transform.position = new Vector3(body.transform.position.x - 0.721236f, body.transform.position.y - 0.3961106f, body.transform.position.z); setanormal.SetActive(true); setaatrito.SetActive(true); } else if (body.transform.position.x > 0.8f && body.transform.position.x < 14) { normalinclinada.transform.position = new Vector3(body.transform.position.x + 0.668203f, body.transform.position.y + 1.48f, body.transform.position.z); atritoinclinada.transform.position = new Vector3(body.transform.position.x - 0.881289f, body.transform.position.y + 0.105972f, body.transform.position.z); normalinclinada.SetActive(true); atritoinclinada.SetActive(true); } else if (body.transform.position.x > 14 && body.transform.position.x < 21.22f) { setanormal.transform.position = new Vector3(body.transform.position.x, body.transform.position.y + 1.48f, body.transform.position.z); setanormal.SetActive(true); if (body.transform.position.x < 16) { setaatrito.transform.position = new Vector3(body.transform.position.x - 0.721236f, body.transform.position.y - 0.3961106f, body.transform.position.z); setaatrito.SetActive(true); } else { setaatrito2.transform.position = new Vector3(body.transform.position.x - 0.721236f, body.transform.position.y - 0.3961106f, body.transform.position.z); setaatrito2.SetActive(true); } } else if (body.transform.position.x > 22.1f && body.transform.position.x < 33.15f) { atrito2inclinada.transform.position = new Vector3(body.transform.position.x - 0.8646961f, body.transform.position.y - 0.0763028f, body.transform.position.z); normalinclinada.transform.position = new Vector3(body.transform.position.x + 0.668203f, body.transform.position.y + 1.48f, body.transform.position.z); normalinclinada.SetActive(true); atrito2inclinada.SetActive(true); } else if (body.transform.position.x > 33.14f && body.transform.position.x < 39.50f) { setanormal.transform.position = new Vector3(body.transform.position.x - 0.00931f, body.transform.position.y + 1.48f, body.transform.position.z); setaatrito2.transform.position = new Vector3(body.transform.position.x - 0.64431f, body.transform.position.y - 0.401f, body.transform.position.z); setanormal.SetActive(true); setaatrito2.SetActive(true); } setapeso.SetActive(true); } else { Time.timeScale = 1; setapeso.SetActive(false); setanormal.SetActive(false); normalinclinada.SetActive(false); setaatrito.SetActive(false); setaatrito2.SetActive(false); setaatrito3.SetActive(false); atritoinclinada.SetActive(false); atrito2inclinada.SetActive(false); atrito3inclinada.SetActive(false); } ispause = !ispause; } }
void Update() { // move right if (Input.GetKeyDown(KeyCode.LeftArrow)) { speed = -defaultSpeed; anim.SetInteger("State", 1); // run if (facingRight) { Flip(); } } if (Input.GetKeyUp(KeyCode.LeftArrow)) { if (speed != defaultSpeed) { speed = 0; } } // move left, stop if (Input.GetKeyDown(KeyCode.RightArrow)) { speed = defaultSpeed; anim.SetInteger("State", 1); // run if (!facingRight) { Flip(); } } if (Input.GetKeyUp(KeyCode.RightArrow)) { if (speed != -defaultSpeed) { speed = 0; } } if (Input.GetKeyDown(KeyCode.UpArrow)) { if (!isJumping && rb.velocity.y >= -0.1f) { isJumping = true; anim.SetTrigger("isJumping"); rb.AddForce(new Vector2(rb.velocity.x, defaultJump)); } } MovePlayer(speed); if (Input.GetKey(KeyCode.X)) { Extract(); } if (Input.GetKey(KeyCode.Z)) { Shoot(); } if (Input.GetKeyDown(KeyCode.LeftShift)) { PaintMyself(); } }
// Прыжок private void Jump() { rigidbody.AddForce(transform.up * jumpforce, ForceMode2D.Impulse); }
void Give_A_Push() { rigidbody2D.AddForce(transform.up * sensativty); }
/// <summary> /// Push the enemy in the direction player is facing. /// </summary> public void PushEnemy() { rgb2D?.AddForce(new Vector2(Mathf.Sign(player.transform.localScale.x) * 1f, 1.5f), ForceMode2D.Impulse); }
public void Move(float move, bool crouch, bool jump) { xInput = move; // If crouching, check to see if the character can stand up if (!crouch) { if (!m_wasCrouching) { m_wasCrouching = true; OnCrouchEvent.Invoke(true); } // If the character has a ceiling preventing them from standing up, keep them crouching if (Physics2D.OverlapCircle(m_CeilingCheck.position, k_CeilingRadius, m_WhatIsGround)) { crouch = true; } } //only control the player if grounded or airControl is turned on if (m_Grounded || m_AirControl) { // If crouching if (crouch) { if (!m_wasCrouching) { m_wasCrouching = true; OnCrouchEvent.Invoke(true); } // Reduce the speed by the crouchSpeed multiplier move *= m_CrouchSpeed; // Disable one of the colliders when crouching if (m_CrouchDisableCollider != null) { m_CrouchDisableCollider.enabled = false; } } else { // Enable the collider when not crouching if (m_CrouchDisableCollider != null) { m_CrouchDisableCollider.enabled = true; } if (m_wasCrouching) { m_wasCrouching = false; OnCrouchEvent.Invoke(false); } } // Move the character by finding the target velocity Vector3 targetVelocity = new Vector2(move * 10f, m_Rigidbody2D.velocity.y); // And then smoothing it out and applying it to the character m_Rigidbody2D.velocity = Vector3.SmoothDamp(m_Rigidbody2D.velocity, targetVelocity, ref velocity, m_MovementSmoothing); // If the input is moving the player right and the player is facing left... if (move > 0 && !m_FacingRight) { // ... flip the player. Flip(); } // Otherwise if the input is moving the player left and the player is facing right... else if (move < 0 && m_FacingRight) { // ... flip the player. Flip(); } if (m_Grounded && Mathf.Abs(move) > 0) { //audioSource.PlayOneShot(m_FootSteps[Random.Range(0,2)], Random.Range(0.50f,0.80f)); //AudioManager.instance.PlayFootStepAudio(m_Grounded); //AudioManager.instance.StopFootStepAudio(); } } // If the player should jump... if (m_Grounded && jump) { // Add a vertical force to the player. m_Grounded = false; m_Rigidbody2D.AddForce(new Vector2(0f, m_JumpForce)); } }
// Update is called once per frame void Update() { horizontalInput = Input.GetAxis("HorizontalP4"); if (horizontalInput < 0f || horizontalInput > 0f) { GetComponent <SpriteRenderer>().flipX = horizontalInput < 0f; arm.GetComponent <SpriteRenderer>().flipX = horizontalInput < 0f; transform.GetChild(1).gameObject.GetComponent <SpriteRenderer>().flipX = horizontalInput < 0f; anim.SetBool("isRunning", true); } else if (horizontalInput == 0) { anim.SetBool("isRunning", false); } //Move Left and Right horizontalInput = Input.GetAxis("HorizontalP4"); if (Input.GetAxis("HorizontalP4") >= 0.90f || Input.GetAxis("HorizontalP4") <= -0.90f) { speed = 15; } else if (Input.GetAxis("HorizontalP4") >= 0.60f || Input.GetAxis("HorizontalP4") <= -0.60f) { speed = 10; } else { speed = 5; } float translation = Input.GetAxis("HorizontalP4") * speed; translation *= Time.deltaTime; transform.Translate(translation, 0, 0); Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.down), Color.blue); //Jump if ((isGrounded == true) && (Input.GetButtonDown("JumpP4") == true)) { jump4.Play(); Vector3 jump = new Vector3(0, jumpspeed, 0); rb.AddForce(jump, ForceMode2D.Impulse); anim.SetBool("isJumping", true); } RaycastHit2D hit; hit = Physics2D.Raycast(transform.position - new Vector3(0, sprite.bounds.extents.y - 0.1f, 0), Vector2.down, 0.5f); if (hit) { isGrounded = true; anim.SetBool("isFalling", false); } else { anim.SetBool("isFalling", true); anim.SetBool("isJumping", false); isGrounded = false; } // Boudning Vector3 minScreenBounds = Camera.main.ScreenToWorldPoint(new Vector3(0, 0, 0)); Vector3 maxScreenBounds = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, 0)); transform.position = new Vector3(Mathf.Clamp(transform.position.x, minScreenBounds.x + 1, maxScreenBounds.x - 1), Mathf.Clamp(transform.position.y, minScreenBounds.y + 1, maxScreenBounds.y - 3), transform.position.z); //Arm movement float angle = Mathf.Atan2(-Input.GetAxis("HorizontalRStickP4"), Input.GetAxis("VerticalRStickP4")) * Mathf.Rad2Deg; if (usingHammer == false) { arm.transform.localPosition = new Vector3(Input.GetAxis("HorizontalRStickP4"), Input.GetAxis("VerticalRStickP4"), 0).normalized; // Rotation of arm arm.transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward); } if (Input.GetAxis("HorizontalRStickP4") >= 0 && usingHammer == true) { arm.transform.localPosition = new Vector3(-1, 0, 0).normalized; arm.transform.rotation = Quaternion.AngleAxis(90, new Vector3(0, 0, 90)); } if (Input.GetAxis("HorizontalRStickP4") <= 0 && usingHammer == true) { arm.transform.localPosition = new Vector3(1, 0, 0).normalized; arm.transform.rotation = Quaternion.AngleAxis(90, new Vector3(0, 0, -90)); } //Rotation of PlayerCircle if ((Input.GetAxis("HorizontalRStickP4") != 0) || (Input.GetAxis("VerticalRStickP4") != 0)) { circle.transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward); } //fading in or out the Aiming circle if ((Input.GetAxis("HorizontalRStickP4") != 0) && isFaded == true || (Input.GetAxis("VerticalRStickP4") != 0) && isFaded == true) { //fade in the aiming circle StartCoroutine("fadeIn"); isFaded = false; } if ((Input.GetAxis("HorizontalRStickP4") == 0) && (Input.GetAxis("VerticalRStickP4") == 0) && isFaded == false) { //fade out the aiming circle StartCoroutine("fadeOut"); isFaded = true; } // Jetpack bar //__________________________________________________________________________________________________________________________________________ rectangle.color = new Color(1f, 1f, 1f, 0f); outerRectangle.color = new Color(1f, 1f, 1f, 0f); if (usingJetpack) { rectangle.color = new Color(1f, 1f, 1f, 1f); outerRectangle.color = new Color(1f, 1f, 1f, 1f); float fuel = childJetpack.GetComponent <WaterJetpack>().JetpackFuel; rectangle.transform.localScale = new Vector3(rectangle.transform.localScale.x, fuel / 500, 1); } }
public void Move(float move, bool crouch, bool jump, bool attack1, bool attack2) { // ground only moveset if (m_Grounded) { if(attack1 || attack2) { isAttacking = true; if (attack1) { m_Anim.SetTrigger("Attack1"); } if (attack2) { m_Anim.SetTrigger("Attack2"); } // Reduce the speed if crouching by the crouchSpeed multiplier move = 0; // The Speed animator parameter is set to the absolute value of the horizontal input. m_Anim.SetFloat("Speed", Mathf.Abs(move)); // Move the character m_Rigidbody2D.velocity = new Vector2(move * m_MaxSpeed, m_Rigidbody2D.velocity.y); } } if (isAttacking) { return; } if (attack1) { m_Anim.SetTrigger("Attack1"); } if (attack2) { m_Anim.SetTrigger("Attack2"); } // If crouching, check to see if the character can stand up if (!crouch && m_Anim.GetBool("Crouch")) { // If the character has a ceiling preventing them from standing up, keep them crouching if (Physics2D.OverlapCircle(m_CeilingCheck.position, k_CeilingRadius, m_WhatIsGround)) { crouch = true; Debug.Log("test"); } } // Set whether or not the character is crouching in the animator m_Anim.SetBool("Crouch", crouch); //only control the player if grounded or airControl is turned on if (m_Grounded || m_AirControl) { // Reduce the speed if crouching by the crouchSpeed multiplier move = (crouch ? move*m_CrouchSpeed : move); // The Speed animator parameter is set to the absolute value of the horizontal input. m_Anim.SetFloat("Speed", Mathf.Abs(move)); // Move the character m_Rigidbody2D.velocity = new Vector2(move*m_MaxSpeed, m_Rigidbody2D.velocity.y); // If the input is moving the player right and the player is facing left... if (move > 0 && !m_FacingRight) { // ... flip the player. Flip(); } // Otherwise if the input is moving the player left and the player is facing right... else if (move < 0 && m_FacingRight) { // ... flip the player. Flip(); } } // If the player should jump... if (m_Grounded && jump && m_Anim.GetBool("Ground")) { // Add a vertical force to the player. m_Grounded = false; m_Anim.SetBool("Ground", false); m_Rigidbody2D.AddForce(new Vector2(0f, m_JumpForce)); } }
void FixedUpdate() { Rigidbody2D ballRB = ball.GetComponent <Rigidbody2D>(); Rigidbody2D charRB = character.GetComponent <Rigidbody2D>(); Vector2 dir = new Vector2(h, v); dir.Normalize(); charRB.MovePosition(charRB.position + dir * Time.fixedDeltaTime * speed); if (following) { float yDiff = (charRB.position.y - ball.transform.position.y) + 1 + idleOffsetY; float xDiff = (charRB.position.x - ball.transform.position.x) + idleOffsetX; // ball.transform.position = new Vector3 (ball.transform.position.x + xDiff/20, ball.transform.position.y + yDiff/20, ball.transform.position.z); ballRB.AddForce(new Vector2(xDiff * 4, yDiff * 4)); if (framesSinceFroze < 1) { // ballRB.AddForce(new Vector2(xDiff*8,yDiff*8)); //add the force again for an initial boost! Vector2 angle = new Vector2(xDiff, yDiff); angle.Normalize(); if (isCharged && !ranOut) { ballRB.AddForce(angle * 700); ballRB.drag = 1; } else if (!ranOut) { ballRB.AddForce(angle * 350 * chargedTime); } ranOut = false; chargedTime = 0; isCharged = false; character.transform.Find("Indicator").GetComponent <SpriteRenderer>().color = inactiveIndicatorColor; framesSinceFroze += 1; } } else { ballRB.velocity = (new Vector2(0, 0)); curMana -= Time.deltaTime * 30; curMana = Mathf.Max(curMana, 0); if (curMana == 0) { ranOut = true; following = true; charging.Stop(); } } if (ballRB.drag < 1.5) { ballRB.drag += (float)Time.deltaTime * .3f; ballRB.drag = Mathf.Min(1.5f, ballRB.drag); print(ballRB.drag); } }
// Start is called before the first frame update void Start() { Vector2 shot = new Vector2(0, -20); rb.AddForce(shot, ForceMode2D.Impulse); }
void FixedUpdate() { rb.AddForce(transform.forward * 10); }
public virtual void Blow(Vector2 pushForce) { rigidBody?.AddForce(pushForce); }
// Update is called once per frame void Update() { newPos = transform.position; isGrounded = Mathf.Abs(rb.velocity.y) < 1e-6; if (Input.GetButtonDown("GirlJump") && isGrounded) { rb.AddForce(new Vector2(0f, jumpSpeed), ForceMode2D.Impulse); jumpAudio.Play(); } anim.SetBool("isJumping", !isGrounded); if (!isGrounded) { anim.SetBool("isRunning", false); anim.SetBool("isWalking", false); } if (isSlide) { moveSpeed = 0f; } else if (Input.GetKey(KeyCode.DownArrow)) { moveSpeed = walkSpeed; } else { moveSpeed = runSpeed; } string stringAction = moveSpeed == walkSpeed ? "isWalking" : "isRunning"; string stringActionNot = moveSpeed != walkSpeed ? "isWalking" : "isRunning"; if (Input.GetKey(KeyCode.A)) { GetComponent <SpriteRenderer>().flipX = true; newPos.x -= (CheckOutOfBound(newPos.x - moveSpeed)) ? moveSpeed: 0; transform.position = newPos; anim.SetBool(stringAction, true); anim.SetBool(stringActionNot, false); if (isSlide) { rb.AddForce(new Vector2(-slideSpeed, 0f), ForceMode2D.Impulse); } } else if (Input.GetKey(KeyCode.D)) { GetComponent <SpriteRenderer>().flipX = false; newPos.x += (CheckOutOfBound(newPos.x + moveSpeed)) ? moveSpeed: 0; transform.position = newPos; anim.SetBool(stringAction, true); anim.SetBool(stringActionNot, false); if (isSlide) { rb.AddForce(new Vector2(slideSpeed, 0f), ForceMode2D.Impulse); } } else { anim.SetBool("isRunning", false); anim.SetBool("isWalking", false); } isRunning = anim.GetBool("isRunning"); isJumping = anim.GetBool("isJumping"); isWalking = anim.GetBool("isWalking"); isDead = anim.GetBool("isDead"); }