【发布时间】:2021-08-05 02:39:06
【问题描述】:
我想根据与枢轴点相关的手指触摸绘制来旋转条。在测试结构实现之后,我创建了它用于测试目的。
目前,我可以编写这段代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestRotateController : MonoBehaviour
{
float rotateSpeed = 10f;
Vector2 touchStartPos;
Transform touchItem;
//
[SerializeField] LayerMask touchItemsMask;
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 mousePos2D = new Vector2(mousePos.x, mousePos.y);
RaycastHit2D hit = Physics2D.Raycast(mousePos2D, Vector2.zero, 0f, touchItemsMask);
if (hit.collider != null && hit.transform.CompareTag(GameConstants.TAG_RELEASE_ANGLE_BAR))
{
touchItem = hit.transform;
touchStartPos = mousePos2D;
}
}
else if (Input.GetMouseButton(0))
{
if (touchItem != null && touchItem.CompareTag(GameConstants.TAG_RELEASE_ANGLE_BAR))
{
Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 mousePos2D = new Vector2(mousePos.x, mousePos.y);
RotateReleaseAngleBar(touchStartPos, mousePos2D);
}
}
else if (Input.GetMouseButtonUp(0))
{
touchItem = null;
}
}
// rotate pivot parent
public void RotateReleaseAngleBar(Vector2 touchStartPosition, Vector2 touchPosition)
{
if (touchStartPosition.x > touchPosition.x)
{
transform.parent.Rotate(Vector3.forward, rotateSpeed * Time.deltaTime);
}
else if (touchStartPosition.x < touchPosition.x)
{
transform.parent.Rotate(Vector3.forward, -rotateSpeed * Time.deltaTime);
}
}
}
现在使用此代码,我无法在手指移动时旋转栏。 选定的方向将保持正确,因为我已使用 X 值来决定这一点,但是当我停止拖动手指时,旋转也会继续沿同一方向。
我想停止这个,我想根据手指拖动量旋转条。这是一种体验,我想要一个人用手指在旋转杆。
【问题讨论】:
标签: unity3d