private void Moving() { var dir = new Vector3(); var ws = Input.GetAxis("Vertical"); var ad = Input.GetAxis("Horizontal"); if (Math.Abs(ws) > 0) { dir += Cam.forward * ws; } if (Math.Abs(ad) > 0) { dir += Cam.right * ad; } if (Input.GetKey(up)) { dir += Cam.up; } if (Input.GetKey(down)) { dir -= Cam.up; } if (Input.GetKey(speedBoost)) { dir *= speedBoostFactor; } Rb.AddForce(dir * speed, ForceMode.Impulse); }
/// <summary> /// Moves to the target aka. the player-base. /// </summary> private void MoveToTarget() { if (Math.Abs(Rb.velocity.x) < maxSpeed) { Rb.AddForce(new Vector2(baseMovementSpeed * Time.fixedDeltaTime * (TargetToLeft ? -1 : 1), 0)); } }
public override void CharacterMove(Vector2 movement) { float moveX = movement.x; float moveY = movement.y; if (Mathf.Abs(moveX) > 0 && Mathf.Abs(moveY) > 0) { if (GlobalVariables.IsHorizontalMovingFirst) { movement = new Vector3(moveX, 0, 0); } else { movement = new Vector3(0, moveY, 0); } } else if (Mathf.Abs(moveX) > 0) { movement = new Vector3(moveX, 0, 0); } else if (Mathf.Abs(moveY) > 0) { movement = new Vector3(0, moveY, 0); } else { Rb.velocity = Vector3.zero; CharacterStateController.SetParams(ISMOVING, false); return; } movement *= BasicMovingSpeed; Rb.AddForce(movement); CharacterStateController.SetParams(ISMOVING, true); }
public override void AddForce(Vector3 netForce, Vector3 allomanticForce) { // Calculate drag from its new velocity // Effectively caps the max velocity of the coin without affecting the ANF. Vector3 newNetForce = netForce; //Vector3 newNetForce = Vector3.ClampMagnitude( // -(Vector3.Project(Rb.velocity, netForce.normalized) + (netForce / NetMass * Time.fixedDeltaTime)) * drag, netForce.magnitude //) + netForce; if (collisionCollider) // If in a collision.. { if (isStuck) // and is stuck... { if (!IsStuckByFriction(netForce) && !IsStuckByFriction(allomanticForce)) // ... but friction is too weak to keep the coin stuck in the target. // Check both netForce and allomanticForce because the APB may decrease // the netForce, even when the allomanticForce would be good enough for friction { UnStick(); } } else // and is not yet stuck from the previous pushes... { isStuck = IsStuckByFriction(netForce) || IsStuckByFriction(allomanticForce); if (isStuck) // but this push would stick the coin. { CreateJoint(collisionCollider.GetComponent <Rigidbody>()); } } } LastExpectedAcceleration = newNetForce / NetMass; // LastPosition, LastVelocity are updated Rb.AddForce(newNetForce * (1 / Time.timeScale)); }
// ReSharper disable once UnusedMember.Local void Move() { float moveH = 0; if (IsGrounded) { moveH = Input.GetAxis("Horizontal") * Speed * Time.deltaTime; } float moveV = Input.GetAxis("Vertical") * Speed * Time.deltaTime; if (!IsGrounded) { moveV *= 0.3f; } if (Input.GetAxis("Vertical") > 0.9) { moveV *= 1.5f; } //Debug.Log("MoveH = " + moveH + " moveV = " + moveV); Rb.AddForce(transform.right * moveH); Rb.AddForce(transform.forward * moveV); Animator.SetBool(CharacterAnimatorState.isMoving.ToString(), Math.Abs(moveV) + Math.Abs(moveH) > 0); Animator.SetBool(CharacterAnimatorState.isWalkingStraight.ToString(), Input.GetAxis("Vertical") > 0); Animator.SetBool(CharacterAnimatorState.isStraffing.ToString(), Math.Abs(Input.GetAxis("Horizontal")) > 0); Animator.SetFloat(CharacterAnimatorState.XWalking.ToString(), Input.GetAxis("Vertical")); Animator.SetFloat(CharacterAnimatorState.YWalking.ToString(), Input.GetAxis("Horizontal")); }
public override void AmmoBehaviourFire() { Rb.AddForce(Direction * Force, ForceMode2D.Impulse); // In case of bug Destroy(gameObject, 10.0f); }
/* * This method is what controls the side sweeping motion * While the key is held down, your vehicle banks and moves in * that direction. * After they are down banking, the vehicle rights itself * Speed is a constant associated with a type of vehicle * Correction is how much the player wants to move sideways * Previous steer what the player inputed last frame. * turning makes sure the player is not turning while banking * */ protected void MoveHorizontal(float speed, float correction, float previousSteer, bool turning) { if (StateManager.curState == 3) { //make sure we only detect landscape int layerMask = 1 << 9; //left side of vehicle RaycastHit lhit; //right side of vehicle RaycastHit rhit; //Getting the ground Physics.Raycast(transform.position + 2 * transform.right, Vector3.down, out rhit, Mathf.Infinity, layerMask); Physics.Raycast(transform.position + -2 * transform.right, Vector3.down, out lhit, Mathf.Infinity, layerMask); //if we turning right if (correction > .8 && !turning) { Rb.AddForceAtPosition(transform.up * 1 / rhit.distance, transform.position + 2 * transform.right); Rb.AddForceAtPosition(transform.up * 1 / lhit.distance * 300, transform.position + -2 * transform.right); Rb.AddForce(transform.right * speed * correction * 20); //left } else if (correction < -.8) { Rb.AddForceAtPosition(transform.up * 1 / rhit.distance * 300, transform.position + 2 * transform.right); Rb.AddForceAtPosition(transform.up * 1 / lhit.distance, transform.position + -2 * transform.right); Rb.AddForce(transform.right * speed * correction * 20); //must bring balance to the forces } else { Rb.AddForceAtPosition(transform.up * 1 / rhit.distance * 15, transform.position + 2 * transform.right); Rb.AddForceAtPosition(transform.up * 1 / lhit.distance * 15, transform.position + -2 * transform.right); } } }
public virtual void StartJump() { State = GlobalVar.State.Jump; xVec = Rb.velocity.x; Rb.AddForce(Vector2.up * GlobalVar.PlayerValue.JumpUpForce); OnGround = false; }
private void Jump() { if (Input.GetKeyDown(jump)) { Rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse); } }
// Use this for initialization protected override void Awake() { base.Awake(); var hinf = new RaycastHit(); Grounded = Observable.EveryFixedUpdate() .Select(_ => Physics.Raycast(RaycastOrigin, Vector3.down, out hinf, Level.Instance.DistBetweenAnchors, LayerMask.GetMask("Bounds", "Blocks", "Characters"))) .ToReadOnlyReactiveProperty(true); Grounded.AddTo(this); Grounded.Subscribe(x => Anim.SetBool("Falling", !x)); ObservableHP = this.ObserveEveryValueChanged(x => x.CurHp) .ToReadOnlyReactiveProperty(CurHp); IsDead = ObservableHP .Select(x => x <= 0) .ToReadOnlyReactiveProperty(false); Rb.OnCollisionEnterAsObservable() .Select(c => c.collider.GetComponentInParent <GameCharacter>()) .Where(gc => gc && (gc.transform.position - transform.position).y < -Level.Instance.DistBetweenAnchors * .5f) .TakeUntil(IsDead.Where(x => x)) .Subscribe(gc => { target.Value = gc; gc.GetHit(); Rb.AddForce(Vector3.up * 3.5f, ForceMode.Impulse); }) .AddTo(this); }
public virtual void AddForce(Vector3 netForce) { if (!IsStatic) { LastExpectedAcceleration = netForce / Mass; Rb.AddForce(netForce, ForceMode.Force); } }
public virtual void AddForce(Vector3 netForce, Vector3 allomanticForce /* unused for the Magnetic base class */) { if (!IsStatic) { LastExpectedAcceleration = netForce / netMass; Rb.AddForce(netForce, ForceMode.Force); } }
/// <summary> /// Jumps to the closest target /// </summary> /// <param name="damageable"></param> protected override void Attack(Damageable damageable) { Rb.AddForce(new Vector2( attackJumpForceX.GetRandom() * (EnemyToLeft ? -1 : 1), attackJumpForceY.GetRandom()) ); _canGetRepulsed = true; }
public void Initialize(float speed, float curving, float?curvingDuration) { Speed = speed; _curving = curving; _curvingDuration = curvingDuration; Rb.AddForce(transform.forward * speed, ForceMode.VelocityChange); _initialized = true; }
/// <summary> Dashes. </summary> /// <param name="dashDistance"> The dash distance. </param> public void Dash(float dashDistance) { Vector3 dashVelocity = Vector3.Scale(transform.forward, dashDistance * new Vector3(Mathf.Log(1f / (Time.deltaTime * Rb.drag + 1)) / -Time.deltaTime, 0, Mathf.Log(1f / (Time.deltaTime * Rb.drag + 1)) / -Time.deltaTime)); Rb.AddForce(dashVelocity, ForceMode.VelocityChange); }
/// <summary> /// Repulses the enemy after the attack /// </summary> private void GetRepulsed() { if (!_canGetRepulsed) { return; } Rb.AddForce(new Vector2(attackRepulseForce.GetRandom() * (EnemyToLeft ? 1 : -1), 0f)); _canGetRepulsed = false; }
private void Update() { //Прыжки if (Input.GetButtonDown("Jump") && Grounded) { Rb.AddForce(new Vector2(0.0f, JumpForce), ForceMode2D.Impulse); } }
public virtual void MoveCtrl() { RaycastHit2D hit = Physics2D.Raycast(Main.position, Vector2.down, 1, GlobalVar.Mask.LayerObj); float slopeAngle = (hit ? Vector2.Angle(hit.normal, Vector2.up) : 0) * (Side ? -1 : 1); if (State == GlobalVar.State.Run || State == GlobalVar.State.Walk || State == GlobalVar.State.Idle) { Rb.AddForce(Math.PosRot(Main.right * (Mathf.Abs(NowInput.MoveAxis) * GlobalVar.PlayerValue.RunForce * MoveForce.Evaluate(Mathf.Abs(Rb.velocity.x) / GlobalVar.PlayerValue.RunForce)), slopeAngle) * Rb.mass); } }
public override void AmmoBehaviourFire() { timer = timerDuration; activate = true; // Negligeable mais je le laisse Rb.AddForce(Direction * Force, ForceMode2D.Impulse); // In case of bug Destroy(gameObject, 10.0f); }
public bool Jump() { if (!isGrounded) { return(false); } groundMagnetismForce = 0f; Rb.AddForce(Vector3.up * jumpPower * Rb.mass, ForceMode.Impulse); return(true); }
public void Initialize(float speed, float sinForce, float sinFrequency, bool fixedSpeed) { Speed = speed; _sinForce = sinForce; _sinFrequency = sinFrequency; _fixedSpeed = fixedSpeed; _currentSinForce = _sinForce; _currentSinFrequency = _sinFrequency; _offset = Time.time; Rb.AddForce(transform.forward * speed, ForceMode.VelocityChange); _initialized = true; }
protected override void Attack() { grounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround); if (Awake) { jumpTimer -= Time.deltaTime; } Anim.SetBool("awake", Awake); Anim.SetBool("grounded", grounded); if (Awake && !Stunned) { if (grounded) { jump = false; if (!jump && jumpTimer <= 0.3f) { Anim.SetBool("prepare", true); } if (!jump && jumpTimer <= 0) { Anim.SetBool("prepare", false); Speed = Player.transform.position.x - Rb.position.x; jumpTimer = jumpCd; Rb.AddForce(new Vector2(0, 700)); jump = true; } } else { Rb.velocity = new Vector2(Speed, Rb.velocity.y); } } if (grounded) { Rb.velocity = new Vector2(-Rb.velocity.x, Rb.velocity.y); if (transform.position.x < Player.transform.position.x && FacingRight) { Flip(); } else if (transform.position.x > Player.transform.position.x && !FacingRight) { Flip(); } } }
public void AddFalling(GameObject Item) { if (Item.GetComponent <Rigidbody2D>() == null) { Rigidbody2D Rb; BoxCollider2D bc; Rb = Item.AddComponent <Rigidbody2D>(); bc = Item.AddComponent <BoxCollider2D>(); float Fx = Random.Range(-100.0f, +100.0f) * 300; float Fy = Random.Range(-100.0f, +100.0f) * 300; Rb.mass = 1; Rb.AddForce(new Vector2(Fx, Fy)); } }
private void HandleInput() { if (Input.GetKeyDown(KeyCode.Space) && (!IsDead) && OnGround) { animator.SetTrigger("jump"); Rb.AddForce(new Vector2(0, jumpForce)); } if (Input.GetKeyDown(KeyCode.Mouse0) && (!IsDead) && !attacking) { animator.SetTrigger("attack"); FindObjectOfType <AudioManager>().Play("slice"); attacking = true; attackTimer = attackCd; } }
public override void Move() { if (!_initialized) { return; } Rb.AddForce(Rb.transform.right * _currentSinForce * Mathf.Cos((Time.time - _offset) * _currentSinFrequency), ForceMode.Acceleration); // If fixed speed: adjust velocity to the constant speed value if (_fixedSpeed) { Rb.velocity = Rb.velocity.normalized * Speed; } _droneModel.transform.LookAt(Rb.position + Rb.velocity); }
public override void StartJump() { State = GlobalVar.State.Jump; OnGround = false; if (GameCtrl.PlayerCtrl.GetState() == GlobalVar.State.Climb) { Vector2 jumpVec = JumpToPos(GameCtrl.PlayerCtrl.GetCenterPos()); if (jumpVec != Vector2.zero) { Rb.velocity = jumpVec; ChangeLayer(GlobalVar.Mask.GetFloor(GameCtrl.Player.gameObject.layer)); return; } } Rb.AddForce(Vector2.up * GlobalVar.PlayerValue.JumpUpForce); }
private void OnCollisionEnter(Collision collision) { if (collision.rigidbody && IsOn) { //Debug.Log(collision.gameObject.name); Vector3 dir = (collision.contacts[0].point - Rb.position + transform.up).normalized; collision.rigidbody.AddForce(dir * 200f, ForceMode.Impulse); Rb.AddForce(-dir * 100f, ForceMode.Impulse); if (collision.gameObject.GetComponent <CBaseParts>()) { collision.gameObject.GetComponent <CBaseParts>().HpDamage(1f); } } }
private void Accelerate(Vector3 direction, float acceleration) { if (isGrounded) { Rb.AddForce(direction * acceleration * Rb.mass, ForceMode.Impulse); } else { float mag = Mathf.Max(new Vector3(Rb.velocity.x, 0, Rb.velocity.z).magnitude, maxFloatingSpeed); Rb.AddForce(direction * acceleration * Rb.mass, ForceMode.Impulse); Vector3 vel = new Vector3(Rb.velocity.x, 0, Rb.velocity.z); vel = Vector3.ClampMagnitude(vel, mag); Rb.velocity = new Vector3(vel.x, Rb.velocity.y, vel.z); } }
// Update is called once per frame protected virtual void Update() { /*if (!haltVelocity) * { * prevXvelocity = Rb.velocity.x; * prevYvelocity = Rb.velocity.y; * Rb.velocity = new Vector2(prevXvelocity, prevYvelocity); * }*/ if (!IsGrounded()) { isFalling = true; } else { isFalling = false; if (!Collider.IsTouching(Floor)) { currentGroundYScale = 1.0f; } else { currentGroundYScale = 0.2f; } } if (Mathf.Abs(Rb.velocity.x) <= MovementSpeed) { Rb.velocity = new Vector2(CalcXVelocity(), Rb.velocity.y); } else { Rb.AddForce(new Vector2(CalcXVelocity(), Rb.velocity.y)); } // Debug.Log(gameObject.name + ": velocity = " + Rb.velocity); if (ShouldJump() && jumpCooldown <= 0) { if (AttemptJump()) { jumpCooldown = timeBetweenJumps; StartCoroutine("JumpCD"); } } }
private void FixedUpdate() { if (_target == null) { return; } if (_target.IsDead) { Rb.velocity = Vector3.zero; return; } _targetPos = _target.transform.position; _targetPos.y += transform.position.y; _currentSpeed = _droneManager.IsFrozen ? 0F : Rb.velocity.magnitude; _direction = (_targetPos - transform.position).normalized; if ((_targetPos - transform.position).magnitude > 0.1f) { Rb.velocity = _direction * _currentSpeed; if (_currentSpeed < Speed) { Rb.AddForce(_direction * Acceleration); } // Don't accelerate over maxSpeed else { _currentSpeed = Speed; Rb.velocity = _direction * _currentSpeed; } if (Mathf.Abs(_currentSpeed) > Mathf.Epsilon) { Rotate(); } } else { Rb.velocity = Vector3.zero; } }