#c# #unity3d
#c# #unity3d
Вопрос:
Когда я перетаскиваю игровой объект за границы экрана, он возвращается к центру сцены, но перестает двигаться сразу после того, как середина игрового объекта пересекает границу экрана.
На этом скриншоте показан игровой объект сразу после остановки при движении к центру сцены.
В чем моя ошибка? Как заставить карту полностью переместиться в центр сцены?
[SerializeField] private Canvas canvas; Vector3 offset = Vector3.zero; [SerializeField] private GameObject cardChoice; private CanvasGroup cardChoiceCG; private int resolutionX = Screen.width; private int resolutionY = Screen.height; [SerializeField] private float distanceToFadeIn; [SerializeField] private float smoothTime = 0.25f; private Vector3 velocity = Vector3.zero; private void Start() { cardChoiceCG = cardChoice.GetComponentlt;CanvasGroupgt;(); } private void Update() { Vector3 screenPosition = canvas.worldCamera.WorldToScreenPoint(this.transform.position); if (screenPosition.x gt; resolutionX || screenPosition.x lt; 0f || screenPosition.y gt; resolutionY || screenPosition.y lt; 0f) { transform.position = Vector3.SmoothDamp(transform.position, canvas.transform.TransformPoint(Vector3.zero), ref velocity, smoothTime); } cardChoiceCG.alpha = Math.Abs((1f / distanceToFadeIn) * (screenPosition.x - (resolutionX / 2))); } public void OnPointerDown(PointerEventData eventData) { RectTransformUtility.ScreenPointToLocalPointInRectangle(canvas.transform as RectTransform, eventData.position, canvas.worldCamera, out Vector2 pos); offset = transform.position - canvas.transform.TransformPoint(pos); } public void OnDrag(PointerEventData eventData) { RectTransformUtility.ScreenPointToLocalPointInRectangle(canvas.transform as RectTransform, eventData.position, canvas.worldCamera, out Vector2 movePos); transform.position = canvas.transform.TransformPoint(movePos) offset; }
}
Комментарии:
1. Не могли бы вы добавить пустой игровой объект в холст и использовать его в качестве цели в функции SmoothDump, возможно, проблема в самом холсте.
Ответ №1:
Решено! transform.position = Vector3.SmoothDamp(transform.position, canvas.transform.TransformPoint(Vector3.zero), ref velocity, smoothTime);
был внутри оператора if, поэтому он работал только тогда, когда центр карты был за пределами экрана.