protected override void Update(float dt) { Vector3 dir = Vector3.Zero; if (Input.IsKeyDown(Keys.W)) { dir = -Vector3.Up; } if (Input.IsKeyDown(Keys.S)) { dir = Vector3.Up; } if (Input.IsKeyDown(Keys.A)) { dir += -Vector3.Right; } if (Input.IsKeyDown(Keys.D)) { dir += Vector3.Right; } _rb.AddForce(dir * Speed); // Continuous rotation Transform.Rotation += dt; }
public override void UpdateState() { base.UpdateState(); _ship.Transform.Rotation = (float)Math.Atan2(_rb.velocity.X, -_rb.velocity.Y); // Check if near the target planet if (Vector2.Distance(_ship.Transform.Position, _ship.Target.Transform.Position) <= 20) { _ship.Target.InsertShip(_ship); _ship.Destroy(); } // Separation from other ships (kind of flocking) foreach (Ship ship in _ship.GameController.Ships) { Vector2 diff = ship.Transform.Position - _ship.Transform.Position; if (diff.Magnitude < 40) { if (ship.Owner == null) { continue; } _rb.AddForce(-diff.Normalized * 4f); } } }
protected override void Update() { Vector3 forces = Vector3.Zero; foreach (var b in Behaviours) { if (b.Enabled) { forces += b.Calculate(this); } } _rb.AddForce(forces); // Normalise velocity if (_rb.velocity.SqrMagnitude > 0.01f * 0.01f) { _direction = _rb.velocity.Normalized; } // Limit velocity if (_rb.velocity.SqrMagnitude > MaxSpeed * MaxSpeed) { _rb.velocity = _rb.velocity.Normalized * MaxSpeed; } }
public static void Simulate(PlayerSimulationConfig config, IRigidbody body, PlayerInput input) { Vector3 forward = body.State.Rotation * Vector3.forward; Vector3 up = body.State.Rotation * Vector3.up; float speedFactor = Mathf.Clamp01((body.State.Velocity.magnitude - 5f) / 10f); float forwardDot = Mathf.Clamp01(1f - forward.y); float rollAngle = MathUtils.AngleAroundAxis(up, Vector3.up, forward); float rollInducedBank = Mathf.Pow(Mathf.Clamp01(Mathf.Abs(rollAngle) * MathUtils.OneOver180 * forwardDot * speedFactor), 1.5f) * Mathf.Sign(rollAngle); float rollStabilizer = Mathf.Pow(Mathf.Clamp01(Mathf.Abs(rollAngle) * MathUtils.OneOver180 * forwardDot), 2f) * Mathf.Sign(rollAngle); rollStabilizer *= 1f - Mathf.Abs(input.Roll); body.AddRelativeTorque(new Vector3( input.Pitch * config.RotationSpeed.x + Mathf.Abs(rollInducedBank) * -0.66f * config.RotationSpeed.x, input.Yaw * config.RotationSpeed.y + rollInducedBank * 0.66f * config.RotationSpeed.y, input.Roll * -config.RotationSpeed.z + rollStabilizer * 0.66f * config.RotationSpeed.z), ForceMode.Force); float maxThrust = input.Boost ? 50f : 20f; body.AddRelativeForce(Vector3.forward * maxThrust * input.Thrust, ForceMode.Force); Vector3 offHeadingVelocity = body.State.Velocity - Vector3.Project(body.State.Velocity, forward); Vector3 dampening = offHeadingVelocity * -2.0f; Vector3 conserved = offHeadingVelocity.magnitude * forward * 0.33f; body.AddForce(dampening + conserved + Vector3.down, ForceMode.Force); }
//This isnt the best for our PlayerRigidbody since our PlayerRigidbody runs in update and this runs in FixedUpdate. void OnTriggerStay(Collider collider) { IRigidbody rigidbody = collider.GetComponent <IRigidbody>(); if (rigidbody != null) { rigidbody.AddForce(direction * force, forceMode); } }
protected override void Update(float dt) { Vector3 dir = (Target.Position - Transform.Position).Normalized; _rb.AddForce(dir * Speed); Transform.Rotation = (float)Math.Atan2(_rb.velocity.X, -_rb.velocity.Y); foreach (var neighbour in Flock.GetNeighbours(this)) { Vector3 diff = neighbour.Transform.Position - Transform.Position; if (Vector3.Magnitude(diff) < 50) { _rb.AddForce(-diff.Normalized * Speed * 0.2f); } } Debug.Track(GameObject); }