Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Smooth moving

Discussion in 'Scripting' started by Prosmatera, Feb 1, 2016.

  1. Prosmatera

    Prosmatera

    Joined:
    Dec 21, 2015
    Posts:
    89
    Hi, guys im trying to make a plane move with a few positions on click i have this code so far
    using UnityEngine;
    using System.Collections;

    public class OnTouchMove : MonoBehaviour {

    void Update()
    {
    if (Input.GetMouseButtonDown(0))
    {
    Vector3 position = transform.position;
    position.z += 2;
    transform.position = position;

    }
    }
    }
    It works well but the movement is not smooth as i wish any help will be appreciated!
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    What do you mean by 'smooth'? This code will move the object 2 units in one frame - is your goal to spread that movement across multiple frames?
     
  3. Craig8726

    Craig8726

    Joined:
    Jul 5, 2013
    Posts:
    79
    try multiplying by delta time it will probably help smooth it out.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class OnTouchMove : MonoBehaviour {
    5.  
    6. void Update()
    7. {
    8. if (Input.GetMouseButtonDown(0))
    9. {
    10. Vector3 position = transform.position;
    11. position.z += 2 *Time.deltaTime;
    12. transform.position = position;
    13.  
    14. }
    15. }
    16. }
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    2 problems:
    1) Note that this is using GetMouseButtonDown - the object only moves the first frame the mouse button is pressed. Since Time.deltaTime varies depending on framerate, this would result in inconsistent movement with each mouse click. If it were changed to GetMouseButton, it would execute eery frame, and move the object at a speed of 2 units per second as long as the mouse is held down.
    2) the statement 2 * Time.deltaTime will result in 0. 2 is an int, so it's multiplying these numbers as ints. You would need to make that 2f * Time.deltaTime.
     
  5. Prosmatera

    Prosmatera

    Joined:
    Dec 21, 2015
    Posts:
    89
    Yes im trying to make it a bit smoother i found this http://docs.unity3d.com/ScriptReference/Vector3.SmoothDamp.html but i really dont know how i can apply it here.
     
    Last edited: Feb 1, 2016
  6. Craig8726

    Craig8726

    Joined:
    Jul 5, 2013
    Posts:
    79
    if you need to be a one click type operation you probably have to use Mathf.lerp or vector3.lerp to interpolate smoothly between to points.
     
  7. Prosmatera

    Prosmatera

    Joined:
    Dec 21, 2015
    Posts:
    89
    Unfortunately its not one click operation
     
  8. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    Well... it is a one click operation right now. How do you want it to actually behave? Describe the behavior you want, in detail.

    "make it a bit smoother" is meaningless.

    Better would be, for example: "When the user clicks, I want it to move 2 units over the course of the next 1 second".

    One of the hardest, but most crucial, parts of programming is being specific enough in describing what you want to accomplish.
     
  9. Prosmatera

    Prosmatera

    Joined:
    Dec 21, 2015
    Posts:
    89
    Sorry i never worked in software industry I've been doing Unity like a hobby i didnt understand one click operation right
    let me try to explain the behavior i want better i want everytime when the user click the plane to move with two units but the movement happens instantly and i want it to look like its sliding im really sorry i cant explain it better im new here. Thank you for the undersanding!
     
  10. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    Alright, so it sounds like my original guess was correct. So the challenge is this: You want something that happens in one frame (the user clicking) to start a function that continues across many frames. The good news is Unity has a feature just for this situation: Coroutines - a function that runs some code, then waits on the engine to tell it when to pick up again (usually the next frame). Coroutines are of the return type 'IEnumerator' and look something like this:
    Code (csharp):
    1. void Update() {
    2. if (Input.GetMouseButtonDown(0) ) {
    3. StartCoroutine(MoveCoroutine() );
    4. }
    5. }
    6.  
    7. IEnumerator MoveCoroutine() {
    8. yield return null; // "wait one frame"
    9. Debug.Log("This happened one frame later");
    10. }
    As for smooth movement, I'll give you another snippet I use often in coroutines:
    Code (csharp):
    1. float duration = 1f; //seconds
    2. for (float t=0f; t < duration; t += Time.deltaTime) {
    3. // do work here; you can use  (t / duration) as the third parameter in any Lerp function
    4. yield return null;
    5. }
    Once you have that, you should be able to insert a Lerp without too much trouble. Here's all the pieces; I've deliberately left them separate so that you can learn by putting them together. :) Good luck!
     
    Prosmatera likes this.
  11. Prosmatera

    Prosmatera

    Joined:
    Dec 21, 2015
    Posts:
    89
    Thank you so much