/// <summary> /// /// </summary> /// <param name="direction"></param> /// <param name="throwInfo"></param> /// <param name="multiplier"></param> /// <returns></returns> private IEnumerator RollCoroutine(Vector2 direction, GrenadeThrowInfo throwInfo, float multiplier) { _rollDirection = direction; var rigidbody = GetComponent <Rigidbody2D>(); var elapsed = 0.0f; var oldPosition = throwInfo.rollCurve.Evaluate(elapsed); while (elapsed < throwInfo.rollDuration) { yield return(new WaitForFixedUpdate()); elapsed += GameplayStatics.gameFixedDeltaTime; var rollPosition = throwInfo.rollCurve.Evaluate(elapsed / throwInfo.rollDuration); var diff = (rollPosition - oldPosition) * throwInfo.rollMoveSpeed; float radians = Mathf.Deg2Rad * Vector2.SignedAngle(Vector2.right, _rollDirection); rigidbody.velocity = new Vector2(Mathf.Cos(radians), Mathf.Sin(radians)) * diff * multiplier; var rotation = diff * throwInfo.rollRotSpeed; if (_rollDirection.x >= 0.0f) { rotation *= -1.0f; } rotation += _subRenderer.transform.localRotation.z; rotation %= 360.0f; var throwPosition = throwInfo.throwCurve.Evaluate(elapsed / throwInfo.rollDuration); _subRenderer.transform.localRotation = Quaternion.Euler(0.0f, 0.0f, rotation * multiplier); _subRenderer.transform.localPosition = new Vector2(0.0f, throwPosition * throwInfo.throwMultiplier) * multiplier; } rigidbody.velocity = Vector2.zero; rigidbody.constraints = RigidbodyConstraints2D.FreezeRotation; }
/// <summary> /// /// </summary> private void Throw(GrenadeThrowInfo throwInfo, float multiplier) { var pickable = Instantiate(_pickablePrefab, character.transform.position, Quaternion.identity).GetComponent <GrenadePickable>(); pickable.Initialize(weaponDescriptor); pickable.isPickable = false; var pickableRenderer = pickable.GetComponent <SpriteRenderer>(); pickableRenderer.sprite = null; var behaviour = pickable.GetComponent <GrenadeBehaviour>(); var renderer = behaviour.subRenderer; if (_grenadeBehaviour != null) { behaviour.CopyFrom(_grenadeBehaviour); behaviour.spriteRenderer = pickableRenderer; } behaviour.RollOnTheFloor(character.targetOrientation, throwInfo, multiplier); character.ClearWeapon(this, false); }
/// <summary> /// /// </summary> /// <param name="direction"></param> /// <param name="throwInfo"></param> /// <param name="multiplier"></param> public void RollOnTheFloor(Vector2 direction, GrenadeThrowInfo throwInfo, float multiplier) { StartCoroutine(RollCoroutine(direction, throwInfo, multiplier)); }