private void ApplyMagnetAttractions(MagnetState state, float deltaSeconds) { if (state != MagnetState.None) { for (int i = 0; i < _magnetTrackingZone.ObjectsInArea.Count; i++) { GameObject go = _magnetTrackingZone.ObjectsInArea[i]; Vector3 positionDifference = Transform.Position - go.Transform.Position; float distanceAttenuationFactor = (float)Math.Pow(1 - (positionDifference.Length() / Radius), 2.0); distanceAttenuationFactor = Math.Max(0, distanceAttenuationFactor); Magnet magnet = go.GetComponent <Magnet>(); Vector3 forceDir = Vector3.Normalize(positionDifference); if (magnet.State != state) { forceDir *= -1f; } float totalStrength = MagnetStrength + magnet.Strength; Vector3 frameImpulse = forceDir * totalStrength * distanceAttenuationFactor * deltaSeconds; magnet.SetCurrentImpulse(frameImpulse); _ballCollider.Entity.LinearMomentum += frameImpulse; Collider otherCollider = go.GetComponent <Collider>(); otherCollider.Entity.LinearMomentum += -frameImpulse; } } }
private new void Start() { base.Start(); state = MagnetState.SHOOT; if (speed == 0) { speed = 10.0f; } if (returnSpeed == 0) { returnSpeed = speed * 2.0f; } body = GetComponent <Rigidbody>(); if (body != null) { body.velocity = transform.forward * speed; } else { Debug.Log("Body for magnet missile not created"); } }
private void SetParticleState(MagnetState state) { if (_magnetParticles != null) { _magnetParticles.ColorTint = state == MagnetState.Red ? RgbaFloat.Red : state == MagnetState.Blue ? RgbaFloat.Blue : RgbaFloat.LightGrey; } }
//_/_/_/_/_/_/_/_/_/_/_/_/_/_/ // CoreCube API < recv > //_/_/_/_/_/_/_/_/_/_/_/_/_/_/ protected override void Recv_sensor(byte[] data) { base.Recv_sensor(data); int type = data[0]; // Motion Sensor https://toio.github.io/toio-spec/docs/2.2.0/ble_sensor if (1 == type) { var _shakeLevel = data[5]; if (_shakeLevel != this.shakeLevel) { this.shakeLevel = _shakeLevel; this.shakeCallback.Notify(this); } } // Magnetic Sensor https://toio.github.io/toio-spec/docs/2.2.0/ble_magnetic_sensor else if (2 == type) { var _magnetState = (MagnetState)data[1]; if (this.magneticSensorRequest != null) { this.magneticSensorRequest.hasReceivedData = true; } this._magnetState = _magnetState; if (this.magneticMode == MagneticMode.MagnetState) { this.magnetStateCallback.Notify(this); } } }
protected override void OnTriggerEnter(Collider other) { if (state == MagnetState.RETURN) { return; } switch (other.gameObject.layer) { case GameLayer.Units: { Unit targetUnit = other.GetComponent <Unit>(); if (targetUnit != null && owner != targetUnit) { float distance = (owner.transform.position - targetUnit.transform.position).magnitude; if (distance <= repellDistance) { targetUnit.body.velocity += owner.view_direction * repellPower; } else { targetUnit.body.velocity += -owner.view_direction * attractPower; } GameObject.Destroy(this.gameObject); Debug.Log("Missile deleted by collision"); return; } break; } case GameLayer.Items: { Item targetItem = other.GetComponent <Item>(); if (targetItem != null && targetItem.magnetic) { state = MagnetState.RETURN; carryItem = targetItem; carryItem.transform.SetParent(transform); carryItem.transform.localPosition = Vector3.zero; if (carryItem.body) { carryItem.body.detectCollisions = false; carryItem.body.useGravity = false; carryItem.body.constraints = RigidbodyConstraints.FreezeAll; } return; } break; } } }
private void ReportUp() { if (currentMagnetState == MagnetState.Down) { currentMagnetState = MagnetState.Up; OnUp(this); if (vibrateOnUp) { Handheld.Vibrate(); } CheckForClick(); } }
private void ReportDown() { if (currentMagnetState == MagnetState.Up) { currentMagnetState = MagnetState.Down; OnDown(this); if (vibrateOnDown) { Handheld.Vibrate(); } clickStartTime = Time.time; } }
public override void Update(float deltaSeconds) { MagnetState newState = MagnetState.None; if (_input.GetMouseButton(Veldrid.Platform.MouseButton.Left)) { newState = MagnetState.Red; } else if (_input.GetMouseButton(Veldrid.Platform.MouseButton.Right)) { newState = MagnetState.Blue; } SetParticleState(newState); ApplyMagnetAttractions(newState, deltaSeconds); }
private void ReportUp() { if (currentMagnetState == MagnetState.Down) { currentMagnetState = MagnetState.Up; OnUp(this); #if UNITY_ANDROID && !UNITY_EDITOR if (vibrateOnUp) Handheld.Vibrate(); #endif CheckForClick(); } }
private void ReportDown() { if (currentMagnetState == MagnetState.Up) { currentMagnetState = MagnetState.Down; OnDown(this); #if UNITY_ANDROID && !UNITY_EDITOR if (vibrateOnDown) Handheld.Vibrate(); #endif clickStartTime = Time.time; } }
GameObject lasthit = null; //last magnetizable object void FixedUpdate() { // Moving the arm. mousePos = Camera.main.ScreenToWorldPoint( new Vector3(Input.mousePosition.x, Input.mousePosition.y, transform.position.z) ); relPos = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position; relPos.Normalize(); rot_z = Mathf.Atan2(relPos.y, relPos.x) * Mathf.Rad2Deg; transform.rotation = Quaternion.Euler(0f, 0f, rot_z + 90); // Magnetism! Magic! if (!Input.GetMouseButton(0)) { magnetState = MagnetState.Off; } switch (magnetState) { // TODO: Switch the threshold logic to just adding a FixedJoint between gun and box on collision case MagnetState.On: hit = Physics2D.CircleCast(tip.position, 1, relPos, magnetStrength, magneticLayer); if (hit && hit.rigidbody.gameObject.GetComponent <BlockController>().blockProperty != BlockController.BlockProperty.Stuck) { //magnetState = MagnetState.Pulling; if (hit.distance > threshold) { // Pull! // TODO: Add pulling particles? hit.rigidbody.velocity = -relPos * magnetSpeed; // magnetStrength; hit.rigidbody.gravityScale = 0; // Gravity removal, so it keeps getting pulled. Physics be damned. } else { // Sticky sticky! attached = hit.rigidbody.gameObject; magnetState = MagnetState.Attached; } } break; //case MagnetState.Pulling: //break; case MagnetState.Attached: //hit.rigidbody.velocity = Vector2.zero; // TODO: Add attached particles to box? attached.transform.parent = transform.parent.transform; // ...just transform? attached.GetComponent <Rigidbody2D>().gravityScale = 0f; // block have stay float -by feihei //attached.transform.position = tip.position; //part of Snap code -by feihei attached.layer = LayerMask.NameToLayer("GrabbedBox"); //there's a lot of prlblem when player able to collide box when it's attached so I make it so it won't - by feihei attached.GetComponent <blockSnap>().snapGrid = null; if (attached.GetComponent <blockSnap>() && snapWhenMagnet) { attached.transform.position = new Vector2(attached.transform.position.x, tip.position.y); attached.GetComponent <blockSnap>().manualSnap(tip.position, attached); } else { attached.transform.position = tip.position; } break; case MagnetState.Off: if (Input.GetMouseButton(0)) { magnetState = MagnetState.On; } if (attached) { attached.layer = attached.GetComponent <BlockController>().orgLayer; attached.transform.parent = null; attached.GetComponent <Rigidbody2D>().gravityScale = attached.GetComponent <BlockController>().orgGravity; // reset gravity. Hard coding is smart, yes? :( } break; } }
void Start() { sr = GetComponent <SpriteRenderer>(); magnetState = MagnetState.Off; }