- Home /
Help regarding "Collect Cubes" Like Player Movement and Rotation
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..
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using DG.Tweening;
 
 public class PlayerController : MonoBehaviour
 {
     private Rigidbody rigidBody;
     private Vector3 screenPoint;
     private Vector3 offset;
     private Vector2 startPos;
     private bool isSwiping;
     private Vector3 direction;
     private Vector2 currentSwipeDelta;
     private Vector2 previousSwipeDelta;
     private Vector3 moveDirection;
     public float movementSpeed;
     private Vector3 newDir;
     private float swipeStartTime;
     private float swipeEndTime;
     private float swipeInterval;
 
     // Start is called before the first frame update
     void Start()
     {
         rigidBody = GetComponent<Rigidbody>();
     }
 
     // Update is called once per frame
     void Update()
     {
         PlayerInput();
     }
 
     private void PlayerInput()
     {
         if (Input.GetMouseButtonDown(0))
         {
             startPos = Input.mousePosition;
             GameManager.Instance.StartGame();
         }
 
         if (Input.GetMouseButton(0))
         {
             isSwiping = true;
             direction = Input.mousePosition - (Vector3)startPos;
             direction = Vector3.Normalize(direction);
         }
 
         if (Input.GetMouseButtonUp(0))
         {
             isSwiping = false;
             direction = Vector2.zero;
         }
 
         currentSwipeDelta = Vector2.zero;
 
         if (isSwiping)
         {
             currentSwipeDelta = Input.mousePosition - (Vector3)startPos;
         }
 
         transform.localPosition = new Vector3(Mathf.Clamp(transform.localPosition.x, -5.9f, 5.9f), transform.position.y, Mathf.Clamp(transform.localPosition.z, -1f, 21f));
     }
 
     private void FixedUpdate()
     {
         if (currentSwipeDelta.magnitude > 30)
         {
             moveDirection.x = direction.x;
             moveDirection.z = direction.y;
             moveDirection.y = 0f;
 
             // Rotation
             Quaternion newRotation = Quaternion.LookRotation(moveDirection);
             Quaternion deltaRotation = Quaternion.Euler(new Vector3(0f, 360f, 0f) * Time.deltaTime);
             rigidBody.MoveRotation(newRotation * deltaRotation);
 
             // Move
             rigidBody.velocity = moveDirection * movementSpeed;
         }
     }
 }
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 :)
Answer by Meishin · Aug 17, 2019 at 09:04 AM
Hello @theAbtahi,
here are some easy tweaks (i'm not sure which one would actually be the one you seek) ;
To smooth direction change (and also velocity change given your script)
 // Smooth directions changes
 float directionSmoothing = 0.5f,
 moveDirection.x = Mathf.Lerp(moveDirection.x, direction.x, directionSmoothing) ;
 moveDirection.z = Mathf.Lerp(moveDirection.z, direction.y, directionSmoothing) ;
 moveDirection.y = 0f;
To avoid lags (typo)
Quaternion deltaRotation = Quaternion.Euler(new Vector3(0f, 360f, 0f) * Time.fixedDeltaTime);
To limit the force applied by other objects on your player, try putting player rigidbody mass to a very high value (tho it might reject objects quite violently). An other way would be to control player position in the LateUpdate function (and not it's rigidbody).
Thanks a lot :). I will try out the velocity and direction smoothing, regarding the rotation ..
 Quaternion deltaRotation = Quaternion.Euler(new Vector3(0f, 360f, 0f) * Time.fixedDeltaTime);
 
I have tried this before, but the result is not what I am looking for
Your answer
 
 
             Follow this Question
Related Questions
Help with rotating a sprite with mouse. 2 Answers
Rotating an object to see everything correctly 0 Answers
How to Add Knockback Force Based on What Rotation it Came From 2 Answers
My enemy character is not facing the player correctly! 0 Answers
FPS Controller rotation,FPS Controller rotation problem 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                