Search Unity

"Collect Cubes" Like Player Movement and Rotation

Discussion in 'Physics' started by theAbtahi, Aug 16, 2019.

  1. theAbtahi

    theAbtahi

    Joined:
    Oct 2, 2015
    Posts:
    3
    Hi fellow game devs,
    I am having difficulty recreating kinda same movement and rotation like the game "Collect Cubes"
    So far I have wrote an script that somewhat works like the game but I haven't yet been able to reach the desired outcome. In Collect cubes the player moves and smoothly rotates along the user touch swipe/drag direction and the smooth transition don't mess with collision or other physics properties.

    Here is what I have done so far..
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using DG.Tweening;
    5.  
    6. public class PlayerController : MonoBehaviour
    7. {
    8.     private Rigidbody rigidBody;
    9.     private Vector3 screenPoint;
    10.     private Vector3 offset;
    11.     private Vector2 startPos;
    12.     private bool isSwiping;
    13.     private Vector3 direction;
    14.     private Vector2 currentSwipeDelta;
    15.     private Vector2 previousSwipeDelta;
    16.     private Vector3 moveDirection;
    17.     public float movementSpeed;
    18.     private Vector3 newDir;
    19.     private float swipeStartTime;
    20.     private float swipeEndTime;
    21.     private float swipeInterval;
    22.  
    23.     // Start is called before the first frame update
    24.     void Start()
    25.     {
    26.         rigidBody = GetComponent<Rigidbody>();
    27.     }
    28.  
    29.     // Update is called once per frame
    30.     void Update()
    31.     {
    32.         PlayerInput();
    33.     }
    34.  
    35.     private void PlayerInput()
    36.     {
    37.         if (Input.GetMouseButtonDown(0))
    38.         {
    39.             startPos = Input.mousePosition;
    40.             GameManager.Instance.StartGame();
    41.         }
    42.  
    43.         if (Input.GetMouseButton(0))
    44.         {
    45.             isSwiping = true;
    46.             direction = Input.mousePosition - (Vector3)startPos;
    47.             direction = Vector3.Normalize(direction);
    48.         }
    49.  
    50.         if (Input.GetMouseButtonUp(0))
    51.         {
    52.             isSwiping = false;
    53.             direction = Vector2.zero;
    54.         }
    55.  
    56.         currentSwipeDelta = Vector2.zero;
    57.  
    58.         if (isSwiping)
    59.         {
    60.             currentSwipeDelta = Input.mousePosition - (Vector3)startPos;
    61.         }
    62.  
    63.         transform.localPosition = new Vector3(Mathf.Clamp(transform.localPosition.x, -5.9f, 5.9f), transform.position.y, Mathf.Clamp(transform.localPosition.z, -1f, 21f));
    64.     }
    65.  
    66.     private void FixedUpdate()
    67.     {
    68.         if (currentSwipeDelta.magnitude > 30)
    69.         {
    70.             moveDirection.x = direction.x;
    71.             moveDirection.z = direction.y;
    72.             moveDirection.y = 0f;
    73.  
    74.             // Rotation
    75.             Quaternion newRotation = Quaternion.LookRotation(moveDirection);
    76.             Quaternion deltaRotation = Quaternion.Euler(new Vector3(0f, 360f, 0f) * Time.deltaTime);
    77.             rigidBody.MoveRotation(newRotation * deltaRotation);
    78.  
    79.             // Move
    80.             rigidBody.velocity = moveDirection * movementSpeed;
    81.         }
    82.     }
    83. }
    84.  
    What I am doing wrong and what needs to be done to achive the smooth movement like Collect Cubes?
    Any help will be appreciated. Thanks in advance :)