private void FixedUpdate() { Vector3 forceToOrigin = /*Vector3.zero*/ -transform.localPosition; physicsObject.AddForce(forceToOrigin); if (Shake && Settings.SHAKE) { physicsObject.AddForce(Random.insideUnitSphere * scale); } Vector3 origin = transform.position; Parallel.For(0, particles.Count, i => { //greatly speeds up the calculation for higher period elements //for (int i = 0, len = particles.Count; i < len; i++) { //{ //find the distance from origin Vector3 diffOrgin = origin - particles[i].PhysicsObj.Position; //calculate the force to center ( clamp is used so particles slow near center Vector3 forceToCenter = Vector3.ClampMagnitude(diffOrgin.normalized * (PARTICLE_SPEED * scale), diffOrgin.sqrMagnitude); particles[i].PhysicsObj.AddForce(forceToCenter); for (int j = 0; j < i; j++) { //find the distance between particles Vector3 diffOther = particles[i].PhysicsObj.Position - particles[j].PhysicsObj.Position; //rare occurance, but seperate from identical other if (diffOther.sqrMagnitude < 0.0001f) { particles[i].PhysicsObj.AddForce(Vector3.one * Mathf.Epsilon); } //calculate the amount of overlap float overlap = diffOther.magnitude - (2 * particles[i].Radius); //radius is the same for both //check if actually overlapping if (overlap < 0) { //add force to seperate Vector3 forceToSeperate = diffOther.normalized * overlap * PARTICLE_SPEED * scale; //apply forces to the particles particles[i].PhysicsObj.AddForce(-forceToSeperate); particles[j].PhysicsObj.AddForce(forceToSeperate); } } //} }); }
public void ApplyForce(Vector2 force) { if (_physics.IsStatic == false) { _physics.AddForce(force); } }
private void Knockback(Vector2 dir) { dir = new Vector2(dir.x, 3.0f); physicsObject.SetVelocity(Vector3.zero); physicsObject.grounded = false; physicsObject.AddForce(dir * Time.deltaTime); }
//Controls the player moving up and down a rope private void VerticalInput() { if (touchingRope) { if (Input.GetKey(KeyCode.UpArrow)) { if (!climbing) { InitClimbing(); } verInput = PlayerCharacter.Instance.ClimbSpeed * Time.deltaTime; } else if (Input.GetKey(KeyCode.DownArrow)) { if (!climbing) { InitClimbing(); } verInput = -PlayerCharacter.Instance.ClimbSpeed * Time.deltaTime; } else { verInput = 0f; } } else if (climbing) //If you're not touching the rope, but still technically climbing { climbing = false; //no longer climbing physicsObject.SetVelY(0f); //reset your velocity if (verInput > 0) //If you were climbing up { physicsObject.AddForce(new Vector3(0f, PlayerCharacter.Instance.ClimbSpeed * Time.fixedDeltaTime / 3.0f, 0f)); //Give the player a little boost so they can get up to the platform } verInput = 0f; physicsObject.enableGravity = true; //Reenable gravity physicsObject.disableGroundedCheck = false; } }
public override void ApplyForce() { // Get the direction between the two objects direction = objectA.Position - objectB.Position; if (direction != Vector3.zero) { // Get the length and direction to calculate spring force currLength = direction.magnitude; direction.Normalize(); // Apply spring force and damping // Hooke's Law: -k * x where k is a constant and x is the length // Damped oscillator: this means the damping is proportional to the // velocity in a harmonic oscillator (spring). Vector3 v = objectA.Velocity - objectB.Velocity; Vector3 force = -stiffness * ((currLength - restLength) * direction); force += -damping *Vector3.Dot(v, direction) * direction; // Apply the equal force to both objects, but opposite directions objectA.AddForce(force); objectB.AddForce(-force); } }
public void AddForceAndNormal(Vector3 force, PhysicsObject offender) { forces.Add(force); offender.AddForce(-force); }
// Update is called once per frame void Update () { if (Input.GetKey(TestKey)) physics.AddForce(TestForce); }