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

How can I Made Smooth Transition of the Object

Discussion in 'Scripting' started by CloudFire, Dec 17, 2015.

  1. CloudFire

    CloudFire

    Joined:
    Dec 8, 2015
    Posts:
    33
    Hi All

    I am new bie to Unity . In my project i have to do when i click the arrow key the object should move to certain distance and come back to the same position. I have a code for that.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class NewBehaviourScript : MonoBehaviour {
    5.  
    6.     // Use this for initialization
    7.     public int speed=20;
    8.     void Start () {
    9.  
    10.     }
    11.  
    12.     // Update is called once per frame
    13.     void Update()
    14.     {
    15.        }
    16.  
    17.         if (Input.GetKeyDown(KeyCode.LeftArrow))
    18.         {
    19.             transform.position += Vector3.right * -speed * Time.deltaTime;
    20.         }
    21.         if (Input.GetKeyUp(KeyCode.LeftArrow))
    22.         {
    23.             transform.position += Vector3.right * speed * Time.deltaTime;
    24.         }
    25.      
    26. }
    But the object goes fast and comes soon.But i want smooth transition. In forums i have found can use Lerp. But no examples. Could any one can help me .How can i do it.
     
    Last edited: Dec 17, 2015
  2. CloudKid

    CloudKid

    Joined:
    Dec 13, 2015
    Posts:
    207
  3. troyfury

    troyfury

    Joined:
    May 15, 2015
    Posts:
    19

    check out MoveTowards and the example they provide: http://docs.unity3d.com/ScriptReference/Vector3.MoveTowards.html
     
  4. CloudFire

    CloudFire

    Joined:
    Dec 8, 2015
    Posts:
    33
    Hi All Thanks troyfury and Cloud Kid for your reply. I have found a solution for smooth transition of an object.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class NewBehaviourScript : MonoBehaviour
    5. {
    6.  
    7. Vector3 startPos;
    8.  
    9.     public float amplitude = 10f;
    10.     public float period = 5f;
    11.  
    12.     protected void Start()
    13.     {
    14.         startPos = transform.position;
    15.     }
    16.  
    17.     protected void Update()
    18.     {
    19.         float theta = Time.timeSinceLevelLoad / period;
    20.         float distance = amplitude * Mathf.Sin(theta);      
    21.         transform.position = startPos + Vector3.left * distance;
    22.     }
    23.  
    24.  
    25. }
    The object is going and coming back to the same position. But I have another problem. In my project i dont want use Mathf.Sin(theta) function. I have to change the code.Could any one can help me..What else i should code instead of
    float distance = amplitude * Mathf.Sin(theta); or any other code also welcome. The object should go smoothly and should come back to same position. Thanks in Advance.
     
  5. CloudFire

    CloudFire

    Joined:
    Dec 8, 2015
    Posts:
    33
    Hi All I have found an Solution.I have attached the Code
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class NewBehaviourScript4 : MonoBehaviour
    5. {
    6.  
    7.  
    8.  
    9.     float m_distanceTraveled = 0f;
    10.     void Update()
    11.     {
    12.  
    13.         if (Input.GetKeyDown("right"))
    14.         {
    15.             StartCoroutine(DoScaleThing());
    16.         }
    17.         if (Input.GetKeyDown("left"))
    18.             StartCoroutine(DoScaleThing1());
    19.         if (Input.GetKeyDown("down"))
    20.             StartCoroutine(DoScaleThing2());
    21.         if (Input.GetKeyDown("up"))
    22.             StartCoroutine(DoScaleThing3());
    23.  
    24.     }
    25.  
    26.     public IEnumerator DoScaleThing()
    27.     {
    28.         while (m_distanceTraveled < 4f)
    29.         {
    30.             Vector3 oldPosition = transform.position;
    31.             transform.Translate(Vector3.right * Time.deltaTime);
    32.             m_distanceTraveled += Vector3.Distance(oldPosition, transform.position);
    33.             yield return null;
    34.         }
    35.         while (m_distanceTraveled > 4f && m_distanceTraveled < 8f)
    36.         {
    37.             Vector3 oldPosition = transform.position;
    38.             transform.Translate(Vector3.right * Time.deltaTime * -1);
    39.             m_distanceTraveled += Vector3.Distance(oldPosition, transform.position);
    40.             yield return null;
    41.         }
    42.  
    43.     }
    44.     public IEnumerator DoScaleThing1()
    45.     {
    46.         while (m_distanceTraveled < 4f)
    47.         {
    48.             Vector3 oldPosition = transform.position;
    49.             transform.Translate(Vector3.left * Time.deltaTime);
    50.             m_distanceTraveled += Vector3.Distance(oldPosition, transform.position);
    51.             yield return null;
    52.         }
    53.         while (m_distanceTraveled > 4f && m_distanceTraveled < 8f)
    54.         {
    55.             Vector3 oldPosition = transform.position;
    56.             transform.Translate(Vector3.left * Time.deltaTime * -1);
    57.             m_distanceTraveled += Vector3.Distance(oldPosition, transform.position);
    58.             yield return null;
    59.         }
    60.  
    61.     }
    62.     public IEnumerator DoScaleThing2()
    63.     {
    64.         while (m_distanceTraveled < 4f)
    65.         {
    66.             Vector3 oldPosition = transform.position;
    67.             transform.Translate(Vector3.back * Time.deltaTime);
    68.             m_distanceTraveled += Vector3.Distance(oldPosition, transform.position);
    69.             yield return null;
    70.         }
    71.         while (m_distanceTraveled > 4f && m_distanceTraveled < 8f)
    72.         {
    73.             Vector3 oldPosition = transform.position;
    74.             transform.Translate(Vector3.back * Time.deltaTime * -1);
    75.             m_distanceTraveled += Vector3.Distance(oldPosition, transform.position);
    76.             yield return null;
    77.         }
    78.  
    79.     }
    80.     public IEnumerator DoScaleThing3()
    81.     {
    82.         while (m_distanceTraveled < 4f)
    83.         {
    84.             Vector3 oldPosition = transform.position;
    85.             transform.Translate(Vector3.forward * Time.deltaTime);
    86.             m_distanceTraveled += Vector3.Distance(oldPosition, transform.position);
    87.             yield return null;
    88.         }
    89.         while (m_distanceTraveled > 4f && m_distanceTraveled < 8f)
    90.         {
    91.             Vector3 oldPosition = transform.position;
    92.             transform.Translate(Vector3.forward * Time.deltaTime * -1);
    93.             m_distanceTraveled += Vector3.Distance(oldPosition, transform.position);
    94.             yield return null;
    95.         }
    96.  
    97.     }
    98.  
    99. }
    When i clicked the four arrow key the object going smoothly going and coming soon.

    But I have another problem. Once i run the code . I can able to click only once.(for eg . In out of 4 keys if i click right arrow the object going right and come back.but after that i click any key it does not work).It Strucks.
    Once again i have to run the project . I have to avoid it.
    In my project i need Each and every time if i click any arrow key. the object should go and come soon.
    It should not struck after the first time . In my code I can run only Once in myproject. What i have to change the code for getting output. Each and Every Time i click any keys the object should go and come smoothly...
     
    Last edited: Dec 22, 2015
  6. CloudFire

    CloudFire

    Joined:
    Dec 8, 2015
    Posts:
    33
    Hi all Any idea of why there is strucking. In my above code i can able to click only once. After that once again i have to run the project. I dont know whats wrong in the code. Could any one help me.

    ANY OTHER CODE ALSO WELCOME. IF I CLICK ARROW KEY ONLY ONCE. THE OBJECT SHOULD GO FRONT (few distance upto 4f) and should come back to the original position.
     
    Last edited: Dec 23, 2015