void Move() { Vector3 nextPartPosition = transform.position; Quaternion nextPartRotation = transform.localRotation; currentPosition = transform.position; currentTouch = Camera.main.ScreenToWorldPoint(Input.mousePosition); // HANDLES ROTATION moveDirection = Utility.GetDirection(currentTouch, currentPosition); currentPosition += moveDirection * distanceBetweenTwoPart; transform.position = Vector2.Lerp(transform.position, currentPosition, Time.deltaTime * Constants.SPEED); // if we have moved and need to rotate if (moveDirection != Vector3.zero) { // calculates the angle we should turn towards, - 90 makes the sprite rotate float targetAngle = Utility.GetAngle(moveDirection); // Mathf.Atan2(moveDirection.y, moveDirection.x) * Mathf.Rad2Deg - 90; // actually rotates the sprite using Slerp (from its previous rotation, to the new one at the designated speed. transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(0, 0, targetAngle), 30f * Time.deltaTime); } snakePart.MoveSnakePart(nextPartPosition, nextPartRotation); if (Vector2.Distance(transform.position, currentTouch) > distanceBetweenTwoPart ) { Move(); } }
public void MoveSnakePart(Vector3 nextPosition, Quaternion nextRotation) { Vector3 nextPartPosition = transform.position; Quaternion nextPartRotation = transform.localRotation; currentPosition = transform.position; transform.position = Vector2.Lerp(transform.position, nextPosition, Time.deltaTime * Constants.SPEED); // HANDLES ROTATION moveDirection = Utility.GetDirection(nextPosition, currentPosition); if (!isLast) { transform.rotation = Quaternion.Slerp(transform.rotation, nextRotation, 20f * Time.deltaTime); snakePart.MoveSnakePart(nextPartPosition, nextPartRotation); } else { moveDirection = Utility.GetDirection(nextPosition, currentPosition); currentPosition += moveDirection * .5f; if (moveDirection != Vector3.zero) { float targetAngle = Utility.GetAngle(moveDirection); // Mathf.Atan2(moveDirection.y, moveDirection.x) * Mathf.Rad2Deg - 90; transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(0, 0, targetAngle), 20f * Time.deltaTime); } } }