Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
2
Question by da_st · Dec 19, 2014 at 10:56 PM · vector3lerptransform.translatesmoothing

Smooth movement using transform.Translate

I am making a 2.5d game with three "paths" lined up along the z axis, the player is moving forward along the x axis, i am trying to make it so that when you swipe up on the screen it moves "25.0f" on the z axis and when you swipe down it does the same downwards. This is the code i am using:

     public float moveCar = 25.0f;
     //First establish some variables
     private Vector3 fp; //First finger position
     private Vector3 lp; //Last finger position
     public float dragDistance;  //Distance needed for a swipe to register
     
     // Update is called once per frame
     void Update()
     {
         //Examine the touch inputs
         foreach (Touch touch in Input.touches)
         {
             if (touch.phase == TouchPhase.Began)
             {
                 fp = touch.position;
                 lp = touch.position;
             }
             if (touch.phase == TouchPhase.Moved)
             {
                 lp = touch.position;
             }
             if (touch.phase == TouchPhase.Ended)
             {
                 //First check if it's actually a drag
                 if (Mathf.Abs(lp.x - fp.x) > dragDistance || Mathf.Abs(lp.y - fp.y) > dragDistance)
                 {   //It's a drag
                     //Now check what direction the drag was
                     //First check which axis
                     if (Mathf.Abs(lp.x - fp.x) > Mathf.Abs(lp.y - fp.y))
                     {   //If the horizontal movement is greater than the vertical movement...
                         if (lp.x>fp.x)  //If the movement was to the right
                         {   //Right move
                             //MOVE RIGHT CODE HERE
 
                         }
                         else
                         {   //Left move
                             //MOVE LEFT CODE HERE
                         
                         }
                     }
                     else
                     {   //the vertical movement is greater than the horizontal movement
                         if (lp.y>fp.y)  //If the movement was up
                         {   //Up move
                             transform.Translate (Vector3.right * moveCar); 
                         }
                         else
                         {   //Down move
                             transform.Translate (Vector3.left * moveCar); 
                         }
                     }
                 }
                 else
                 {   //It's a tap
                     //TAP CODE HERE
                     transform.Translate (Vector3.up * 6);
 
                 }

This does what i want, except that the movement isn't smooth at all, it just kind of teleports to the new location, i was wondering how i could make it move to the new position smoother. I have tried looking into the Vector3.Lerp but didnt really understand how to implement it. Any tips on how i could achieve the smoother movement effect? :)

Comment
Add comment · Show 2
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 TheAlmightyPixel · Dec 21, 2014 at 08:05 PM 0
Share

You could use Time.deltaTime, but it should be called from a loop or Update(). What you're doing now is basically moving the player to a direction by the value of 'moveCar'. What you should do is use

 transform.Translate(Vector.right * moveCar * Time.deltaTime)

Perhaps you could use a bool to check if the player is supposed to be moved, and trigger a movement function from Update()?

avatar image da_st · Jan 12, 2015 at 08:58 AM 0
Share

Hey sorry for taking so long to respond! I tried with Time.deltatTime, but now ins$$anonymous$$d of moving smoother it just moves shorter distance than before :/

2 Replies

· Add your reply
  • Sort: 
avatar image
5

Answer by UziMonkey · Jan 12, 2015 at 09:45 AM

First, you'll need to store the destination location to move to over time. This is not something that can be done in a single update call, and you'll need to store the state between frames. So you can start by putting in something like this.

 public Vector3 destination;

Initialize that in Start to your current location, so it doesn't try to move right away.

 void Start() {
   destination = transform.location;
 }

And now instead of immediately translating to your destination, in your input detection code all you need to do is something like this.

 destination += Vector3.left * moveCar;

You're all set up to go, now the Vector3.Lerp part. Vector3.Lerp will return a point on a line between two vectors. The "time" parameter you give it is something between 0 and 1, at 0 it'll return the first vector, at 1 it'll return the second vector, at 0.5 a point in the middle of the two vectors, etc. But you don't need to keep track of time. There's a clever way of doing this.

 transform.position = Vector3.Lerp(transform.position, destination, 0.5f * Time.deltaTime);

In this code, instead of keeping track of how far we've gone and how long we have to get there, we continually pass 0.5f (multiplied by Time.deltaTime, since we're doing this every frame), so we're continually aiming for a point halfway between where the object is this frame, and where we want to go. The first frame you'll accelerate quickly. As you start to get closer, you'll continually slow down as the halfway point gets closer and closer to the destination. It's very simple, and it's certainly not the best way, but I think you'll find this script useful.

 using UnityEngine;
 using System.Collections;
 
 public class SmoothMove : MonoBehaviour {
     public Vector3 destination;
     public float speed = 0.1f;
 
     void Start () {
         destination = transform.position;
     }
     
     void Update () {
         transform.position = Vector3.Lerp(transform.position, destination, speed * Time.deltaTime);
     }
 }
 

Simply attach this to an object, set the destination Vector3 and it'll move there relatively smoothly.

Comment
Add comment · Show 2 · 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 da_st · Jan 12, 2015 at 10:39 AM 0
Share

Hey thanks this was helpful, but one issue is that now the player is unable to move forward on the X axis, it kind of is stuck to one position on x? :)

avatar image Aziz1989 da_st · Oct 30, 2015 at 01:48 PM 0
Share

Hey did you find a good solution for this i have the sale exacte problem please help

avatar image
1

Answer by spenzhg · Apr 18, 2016 at 07:14 AM

I would suggest using a coroutine instead of changing the transform through Update. Using a coroutine makes the code look nicer and there's significantly less overhead. (Read more here: Unity Docs)

Here is a generic example that moves an object smoothly in the x direction by 1 unit when someone hits the right arrow key (Attach this to what you want to move.):

 using UnityEngine;
 using System.Collections;
 
 public class Move_EX : MonoBehaviour {
     Vector3 right = new Vector3 (1f, 0, 0); //Vector in the direction you want to move in.
 
     void Update () { 
         if (Input.GetKeyDown ("right")) {//Using update to capture the keypress.
             StartCoroutine (smooth_move (right, 1f)); //Calling the coroutine.
         }
     }
 
 
     IEnumerator smooth_move(Vector3 direction,float speed){
         float startime = Time.time;
         Vector3 start_pos = transform.position; //Starting position.
         Vector3 end_pos = transform.position + direction; //Ending position.
 
         while (start_pos != end_pos && ((Time.time - startime)*speed) < 1f) { 
             float move = Mathf.Lerp (0,1, (Time.time - startime)*speed);
 
             transform.position += direction*move;
 
             yield return null;
         }
     }
 }
 


Comment
Add comment · Show 2 · 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 GeneralRAAM · May 19, 2017 at 09:33 AM 0
Share

Thank you for this code, honestly it worked perfectly and it was what I was looking for :D

avatar image dinesh-jadhav · Jun 19, 2017 at 02:16 PM 0
Share

Hello Sir, This code works great for smooth movement. But object is not moving with constant speed.

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

8 People are following this question.

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

Related Questions

3 coroutines to ease in lerp, MoveToward steady pace, and ease out lerp 0 Answers

Smooth touch for map 0 Answers

Collision.contacts? 1 Answer

Stop a Lerp from looping 3 Answers

How to Gradually Increase Speed with Lerp or Slerp? 1 Answer


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