Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by theAbtahi · Aug 16, 2019 at 09:02 PM · rotationscripting problemmovementphysic

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"

  • https://play.google.com/store/apps/details?id=com.cinna.collectcubes&hl=en

  • https://apps.apple.com/us/app/collect-cubes/id1465222322

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 :)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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).

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image theAbtahi · Aug 18, 2019 at 01:38 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

251 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges