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 /
  • Help Room /
avatar image
0
Question by mghffel · Jan 04, 2017 at 10:53 PM · 2dmovementgridgridmovesmoothly

Need to make grid movement more smooth

I am building a top down grid style game where the player is controlled by the arrow keys and one key press in a direction moves the character one block in that direction. The code I have now works fine for getting the actual result, but I would like the transition from block to block to be more smooth, so instead of just teleporting from one block to the next I would like it to kinda slide into the next block.

Here is my code

 public float timeBetweenMoves = 0.3333f; 
         private float timestamp;
     
     void Update () {
             ArrowKeyMovement();
         }
     
     void ArrowKeyMovement()
         {
             if (Time.time >= timestamp)
             {
                 if (Input.GetKey("up"))
                 {
                     transform.position = new Vector3(transform.position.x, transform.position.y + 1);
                     transform.eulerAngles = new Vector3(0, 0, 0);
                     timestamp = Time.time + timeBetweenMoves;
                 }
                 else if (Input.GetKey("down"))
                 {
                     transform.position = new Vector3(transform.position.x, transform.position.y - 1);
                     transform.eulerAngles = new Vector3(0, 0, 180);
                     timestamp = Time.time + timeBetweenMoves;
                 }
                 else if (Input.GetKey("left"))
                 {
                     transform.position = new Vector3(transform.position.x - 1, transform.position.y);
                     transform.eulerAngles = new Vector3(0, 0, 90);
                     timestamp = Time.time + timeBetweenMoves;
                 }
                 else if (Input.GetKey("right"))
                 {
                     transform.position = new Vector3(transform.position.x + 1, transform.position.y);
                     transform.eulerAngles = new Vector3(0, 0, 270);
                     timestamp = Time.time + timeBetweenMoves;
                 }
             }
         }

I have tried things like this, but they don't fix the problem.

 if (Input.GetKey("up"))
             {
                 //transform.position = new Vector3(transform.position.x, transform.position.y + 1);
                 for (int i = 0; i < 50; i++)
                 {
                     transform.position = new Vector3(transform.position.x, transform.position.y + .02f);
                 }
                 transform.eulerAngles = new Vector3(0, 0, 0);
                 timestamp = Time.time + timeBetweenMoves;
             }

Thanks for any help!

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
1
Best Answer

Answer by KoenigX3 · Jan 05, 2017 at 12:34 AM

You need to use the Vector3.Lerp function, which stands for linear interpolation.

First, declare a new Vector3 variable, named as desiredPosition. At each Input.GetKey method this Vector3 will be updated with new coordinates instead of changing the position of the gameobject.

After handling keyboard input, you can change the position of the gameobject with the Vector3.Lerp function. The method will smoothly translate the gameobject from the initial position to the desired position. The speed can be changed by the interpolationSpeed value.

You can also interpolate the eulerAngles of the transform with the same method. Be sure to use a new Vector3 variable and update it each time you press a movement button.

     public float timeBetweenMoves = 0.3333f; 
     private float timestamp;
     public float interpolationSpeed = 10.0F;
     Vector3 desiredPosition;
 
     void Start()
     {
         desiredPosition = transform.position;
     }
 
     void Update () 
     {
         ArrowKeyMovement();
         transform.position = Vector3.Lerp(transform.position, desiredPosition, interpolationSpeed * Time.deltaTime);
     }
 
     void ArrowKeyMovement()
     {
         if (Time.time >= timestamp)
         {
             if (Input.GetKey("up"))
             {
                 desiredPosition += Vector3.up;
                 transform.eulerAngles = new Vector3(0, 0, 0);
                 timestamp = Time.time + timeBetweenMoves;
             }
             else if (Input.GetKey("down"))
             {
                 desiredPosition -= Vector3.up;
                 transform.eulerAngles = new Vector3(0, 0, 180);
                 timestamp = Time.time + timeBetweenMoves;
             }
             else if (Input.GetKey("left"))
             {
                 desiredPosition += Vector3.left;
                 transform.eulerAngles = new Vector3(0, 0, 90);
                 timestamp = Time.time + timeBetweenMoves;
             }
             else if (Input.GetKey("right"))
             {
                 desiredPosition += Vector3.right;
                 transform.eulerAngles = new Vector3(0, 0, 270);
                 timestamp = Time.time + timeBetweenMoves;
             }
         }
     }
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 mghffel · Jan 05, 2017 at 03:45 AM 0
Share

This did exactly what I wanted it to. Thank you!

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

137 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

Related Questions

The object moves more that what I want (MoveTowards) 0 Answers

Grid Movement with Smooth Movement 0 Answers

How can I make the character jump between the boxes? 0 Answers

Is ther a way to base movement translation through the grid map of the tilemap? 1 Answer

Raycast is hitting enemy even when not in contact with it? 2 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