Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Docs/VS_Scratch_Mapping.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Scratch ブロックと FUnity 独自 Visual Scripting Unit の対応関係で
| ○度に向ける | FUnity.Runtime.Integrations.VisualScripting.Units.ScratchUnits.PointDirectionUnit | ○度に向ける | FUnity/Scratch/動き | 向きを絶対設定。定義: Runtime/.../TurnAndPointUnits.cs |
| マウスポインターへ向ける | FUnity.Runtime.Integrations.VisualScripting.Units.ScratchUnits.PointTowardsMousePointerUnit | マウスポインターへ向ける | FUnity/Scratch/動き | カーソル方向へ即時旋回。定義: Runtime/.../TurnAndPointUnits.cs |

> **角度の扱い:** Runtime/Integrations/VisualScripting/Units/ScratchUnits/ScratchUnitUtil.cs の `GetDirectionDegreesForCurrentMode` を経由し、Scratch モードでは上=0°/右=90°/左=-90°/下=±180°、通常モードでは従来通り右=0° の角度ルールを適用しています。`DirFromDegrees` も同ユーティリティでモード差を吸収します
> **角度の扱い:** Runtime/Integrations/VisualScripting/Units/ScratchUnits/ScratchUnitUtil.cs の `GetDirectionDegreesForCurrentMode` を経由し、Scratch モードでは上=0°/右=90°/左=-90°/下=±180°、通常モードでは従来通り右=0° の角度ルールを適用しています。最終的に Presenter や ActorState へ渡す際は Runtime/Core/ScratchAngleUtil.cs を使い、必ず内部角度(0°=右)へ変換してから `ActorPresenterAdapter.SetDirection` に渡します

## 見た目
| Scratch ブロック (日本語) | FUnity Unit クラス | UnitTitle | UnitCategory | 備考 |
Expand Down
33 changes: 33 additions & 0 deletions Runtime/Core/ScratchAngleUtil.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Updated: 2025-10-23
using UnityEngine;

namespace FUnity.Runtime.Core
{
/// <summary>
/// Scratch角度(0°=上)と内部角度(0°=右)を相互変換するユーティリティです。
/// Visual Scripting の Scratch 用ユニットでは Scratch角度で計算し、
/// Presenter や ActorState に渡す直前で内部角度へ揃えるために利用します。
/// </summary>
public static class ScratchAngleUtil
{
/// <summary>
/// Scratch角度(0°=上)を内部角度(0°=右)へ変換します。
/// </summary>
/// <param name="scratchDeg">Scratch角度(度)。</param>
/// <returns>内部角度(度)。</returns>
public static float ScratchToInternal(float scratchDeg)
{
return Mathf.Repeat(90f - scratchDeg, 360f);
}

/// <summary>
/// 内部角度(0°=右)を Scratch角度(0°=上)へ変換します。
/// </summary>
/// <param name="internalDeg">内部角度(度)。</param>
/// <returns>Scratch角度(度)。</returns>
public static float InternalToScratch(float internalDeg)
{
return Mathf.Repeat(90f - internalDeg, 360f);
}
}
}
10 changes: 5 additions & 5 deletions Runtime/Integrations/VisualScripting/ActorPresenterAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class ActorPresenterAdapter : MonoBehaviour
[SerializeField]
private ActorPresenter m_ActorPresenter;

/// <summary>Presenter 未接続時に保持する向き(度)。0=右, 90=上, 180=左, 270=下。</summary>
/// <summary>Presenter 未接続時に保持する内部角度(度)。0=右, 90=上, 180=左, 270=下。</summary>
[SerializeField]
private float m_LocalDirectionDeg = 90f;

Expand Down Expand Up @@ -179,9 +179,9 @@ public void SetActorPresenter(ActorPresenter presenter)
}

/// <summary>
/// 現在の向きを度単位で設定する。0=右90=上180=左270=下 として扱う
/// 現在の向きを内部角度(0=右, 90=上, 180=左, 270=下)として設定します
/// </summary>
/// <param name="degrees">設定する角度(度)。</param>
/// <param name="degrees">設定する内部角度(度)。</param>
public void SetDirection(float degrees)
{
m_LocalDirectionDeg = degrees;
Expand Down Expand Up @@ -211,9 +211,9 @@ public void SetRotationStyle(RotationStyle style)
}

/// <summary>
/// 現在の向きを度単位で取得する。0=右90=上180=左270=下 を想定する
/// 現在の向きを内部角度(0=右, 90=上, 180=左, 270=下)として取得します
/// </summary>
/// <returns>現在の向き(度)。</returns>
/// <returns>現在の内部角度(度)。</returns>
public float GetDirection()
{
if (m_ActorPresenter == null)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections;
using UnityEngine;
using Unity.VisualScripting;
using FUnity.Runtime.Core;
using FUnity.Runtime.Model;
using FUnity.Runtime.Integrations.VisualScripting;

Expand Down Expand Up @@ -71,7 +72,11 @@ private IEnumerator Run(Flow flow)
yield break;
}

var uiDirection = ScratchUnitUtil.DirFromDegrees(adapter.GetDirection());
var currentInternal = adapter.GetDirection();
var currentForMode = FUnityModeUtil.IsScratchMode
? ScratchAngleUtil.InternalToScratch(currentInternal)
: currentInternal;
var uiDirection = ScratchUnitUtil.DirFromDegrees(currentForMode);
if (uiDirection.sqrMagnitude <= Mathf.Epsilon)
{
uiDirection = new Vector2(0f, -1f);
Expand All @@ -89,10 +94,13 @@ private IEnumerator Run(Flow flow)
yield break;
}

var reflectedDeg = ScratchUnitUtil.DegreesFromUiDirection(bouncedDirection);
var reflectedForMode = ScratchUnitUtil.DegreesFromUiDirection(bouncedDirection);
var reflectedInternal = FUnityModeUtil.IsScratchMode
? ScratchAngleUtil.ScratchToInternal(reflectedForMode)
: reflectedForMode;
var finalUi = adapter.ToUiPosition(clampedCenter);

adapter.SetDirection(reflectedDeg);
adapter.SetDirection(reflectedInternal);
adapter.SetPositionPixels(finalUi);

yield return m_Exit;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections;
using UnityEngine;
using Unity.VisualScripting;
using FUnity.Runtime.Core;
using FUnity.Runtime.Integrations.VisualScripting;

namespace FUnity.Runtime.Integrations.VisualScripting.Units.ScratchUnits
Expand Down Expand Up @@ -86,8 +87,11 @@ private IEnumerator Run(Flow flow)
var view = controller.ActorView;
var rootScaledSize = view != null ? view.GetRootScaledSizePx() : Vector2.zero;
var currentCenter = presenter.GetPosition();
var directionDeg = controller.GetDirection();
var uiDirection = ScratchUnitUtil.DirFromDegrees(directionDeg);
var directionInternal = controller.GetDirection();
var directionForMode = FUnityModeUtil.IsScratchMode
? ScratchAngleUtil.InternalToScratch(directionInternal)
: directionInternal;
var uiDirection = ScratchUnitUtil.DirFromDegrees(directionForMode);
if (uiDirection.sqrMagnitude <= Mathf.Epsilon)
{
uiDirection = new Vector2(0f, -1f);
Expand All @@ -102,7 +106,7 @@ private IEnumerator Run(Flow flow)

var remaining = movePixels;
var hasBounced = false;
var nextDirectionDeg = directionDeg;
var nextDirectionInternal = directionInternal;
var logicalDirection = new Vector2(uiDirection.x, -uiDirection.y);
logicalDirection.Normalize();

Expand Down Expand Up @@ -136,7 +140,10 @@ private IEnumerator Run(Flow flow)
uiDirection = bouncedDirection;
logicalDirection = new Vector2(uiDirection.x, -uiDirection.y);
logicalDirection.Normalize();
nextDirectionDeg = ScratchUnitUtil.DegreesFromUiDirection(uiDirection);
var nextDirectionForMode = ScratchUnitUtil.DegreesFromUiDirection(uiDirection);
nextDirectionInternal = FUnityModeUtil.IsScratchMode
? ScratchAngleUtil.ScratchToInternal(nextDirectionForMode)
: nextDirectionForMode;
continue;
}

Expand All @@ -148,7 +155,7 @@ private IEnumerator Run(Flow flow)

if (hasBounced)
{
controller.SetDirection(nextDirectionDeg);
controller.SetDirection(nextDirectionInternal);
}

yield return m_Exit;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections;
using UnityEngine;
using Unity.VisualScripting;
using FUnity.Runtime.Core;
using FUnity.Runtime.Integrations.VisualScripting;
using FUnity.Runtime.Presenter;

Expand Down Expand Up @@ -57,19 +58,19 @@ protected override void Definition()
/// <returns>後続へ制御を渡す列挙子。</returns>
private IEnumerator Run(Flow flow)
{
var delta = flow.GetValue<float>(m_DeltaDegrees);
var deltaScratchDeg = flow.GetValue<float>(m_DeltaDegrees);

var presenterObj = ScratchUnitUtil.GetPresenter(flow);
if (presenterObj != null)
{
VSPresenterBridge.TurnSelf(presenterObj, delta);
VSPresenterBridge.TurnSelf(presenterObj, deltaScratchDeg);
}
else
{
var bridge = VSPresenterBridge.Instance;
if (bridge != null)
{
bridge.TurnDegrees(delta);
bridge.TurnDegrees(deltaScratchDeg);
}
else
{
Expand All @@ -85,8 +86,15 @@ private IEnumerator Run(Flow flow)
yield break;
}

var current = controller.GetDirection();
controller.SetDirection(current + delta);
var currentInternal = controller.GetDirection();
var currentForMode = FUnityModeUtil.IsScratchMode
? ScratchAngleUtil.InternalToScratch(currentInternal)
: currentInternal;
var nextForMode = currentForMode + deltaScratchDeg;
var nextInternal = FUnityModeUtil.IsScratchMode
? ScratchAngleUtil.ScratchToInternal(nextForMode)
: nextForMode;
controller.SetDirection(nextInternal);
yield return m_Exit;
}

Expand Down Expand Up @@ -150,8 +158,11 @@ private IEnumerator Run(Flow flow)
yield break;
}

var degrees = flow.GetValue<float>(m_Degrees);
controller.SetDirection(degrees);
var scratchDeg = flow.GetValue<float>(m_Degrees);
var internalDeg = FUnityModeUtil.IsScratchMode
? ScratchAngleUtil.ScratchToInternal(scratchDeg)
: scratchDeg;
controller.SetDirection(internalDeg);
yield return m_Exit;
}
}
Expand Down Expand Up @@ -228,8 +239,11 @@ private IEnumerator Run(Flow flow)
yield break;
}

var degrees = ScratchUnitUtil.GetDirectionDegreesForCurrentMode(delta);
adapter.SetDirection(degrees);
var directionForMode = ScratchUnitUtil.GetDirectionDegreesForCurrentMode(delta);
var internalDeg = FUnityModeUtil.IsScratchMode
? ScratchAngleUtil.ScratchToInternal(directionForMode)
: directionForMode;
adapter.SetDirection(internalDeg);
yield return m_Exit;
}
}
Expand Down