Синтаксис:
Используется csharp
{
[SerializeField]
[Range(0.01f, 0.99f)]
float intensity = 0.1f;
Rigidbody rb;
Vector2 startTouchDirection;
Vector3 startObjectDirection;
void Start()
{
rb = GetComponent<Rigidbody>();
Physics.gravity = Vector3.back * 9.81f;
}
void Update()
{
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
switch (touch.phase)
{
case TouchPhase.Began:
startTouchDirection = GetTouchDirection(touch);
startObjectDirection = objectDirection;
break;
case TouchPhase.Moved:
var touchAngle = Vector2.SignedAngle(startTouchDirection, GetTouchDirection(touch));
var targetDirection = Quaternion.AngleAxis(touchAngle, axis) * startObjectDirection;
rb.angularVelocity = axis * Vector3.SignedAngle(objectDirection, targetDirection, axis);
rb.angularVelocity *= intensity;
break;
case TouchPhase.Ended:
rb.angularVelocity = Vector3.zero;
break;
case TouchPhase.Canceled:
rb.angularVelocity = Vector3.zero;
break;
}
}
#if UNITY_EDITOR
if (Input.GetMouseButtonDown(0))
{
startTouchDirection = mouseDirection;
startObjectDirection = objectDirection;
}
else if (Input.GetMouseButton(0))
{
var touchAngle = Vector2.SignedAngle(startTouchDirection, mouseDirection);
var targetDirection = Quaternion.AngleAxis(touchAngle, axis) * startObjectDirection;
rb.angularVelocity = axis * Vector3.SignedAngle(objectDirection, targetDirection, axis);
rb.angularVelocity *= intensity;
}
else if (Input.GetMouseButtonUp(0))
rb.angularVelocity = Vector3.zero;
#endif
}
Vector3 axis { get; } = Vector3.down; // Ось вращения
Vector3 objectDirection => transform.forward; // Вектор сравнения (возможно тут нужен forward)
Vector2 halfScreenSize => new Vector2(Screen.width / 2.0f, Screen.height / 2.0f);
Vector2 GetTouchDirection(Touch touch) => touch.position - halfScreenSize;
Vector2 mouseDirection => (Vector2)Input.mousePosition - halfScreenSize;
}
[SerializeField]
[Range(0.01f, 0.99f)]
float intensity = 0.1f;
Rigidbody rb;
Vector2 startTouchDirection;
Vector3 startObjectDirection;
void Start()
{
rb = GetComponent<Rigidbody>();
Physics.gravity = Vector3.back * 9.81f;
}
void Update()
{
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
switch (touch.phase)
{
case TouchPhase.Began:
startTouchDirection = GetTouchDirection(touch);
startObjectDirection = objectDirection;
break;
case TouchPhase.Moved:
var touchAngle = Vector2.SignedAngle(startTouchDirection, GetTouchDirection(touch));
var targetDirection = Quaternion.AngleAxis(touchAngle, axis) * startObjectDirection;
rb.angularVelocity = axis * Vector3.SignedAngle(objectDirection, targetDirection, axis);
rb.angularVelocity *= intensity;
break;
case TouchPhase.Ended:
rb.angularVelocity = Vector3.zero;
break;
case TouchPhase.Canceled:
rb.angularVelocity = Vector3.zero;
break;
}
}
#if UNITY_EDITOR
if (Input.GetMouseButtonDown(0))
{
startTouchDirection = mouseDirection;
startObjectDirection = objectDirection;
}
else if (Input.GetMouseButton(0))
{
var touchAngle = Vector2.SignedAngle(startTouchDirection, mouseDirection);
var targetDirection = Quaternion.AngleAxis(touchAngle, axis) * startObjectDirection;
rb.angularVelocity = axis * Vector3.SignedAngle(objectDirection, targetDirection, axis);
rb.angularVelocity *= intensity;
}
else if (Input.GetMouseButtonUp(0))
rb.angularVelocity = Vector3.zero;
#endif
}
Vector3 axis { get; } = Vector3.down; // Ось вращения
Vector3 objectDirection => transform.forward; // Вектор сравнения (возможно тут нужен forward)
Vector2 halfScreenSize => new Vector2(Screen.width / 2.0f, Screen.height / 2.0f);
Vector2 GetTouchDirection(Touch touch) => touch.position - halfScreenSize;
Vector2 mouseDirection => (Vector2)Input.mousePosition - halfScreenSize;
}