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 husnain_rao · Dec 29, 2017 at 12:10 PM · animationtranslatetransitionsdelta position

To change object Position Smoothly

hey Firends! i am working on a game in which i want my object to change its position verticaly after trigger. i have done this, but when it is triggered it jump suddnely. i want it to change its position smoothly using some time And Here is my Code

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.SceneManagement;
 using UnityEngine.UI;
 
 public class Target : MonoBehaviour {
     // Use this for initialization
     public Text text;
     public int maxHits = 3;
     public int CountScore=0;
     public float moveAmount;
     public Vector2 TargetPosition;
     public Ball ball; 
     static float t = 0.0f;
     void Start(){
         TargetPosition = transform.position;
         Debug.Log ("first position" + TargetPosition);
     }
     void OnTriggerEnter2D (Collider2D Collision) {
         CountScore++;
         Debug.Log (CountScore);
         //Moving Target position According to hits
         if (maxHits!=null) {
             TargetPosition.y += moveAmount;
             MoveTargetPosition ();
             maxHits--;
         }
     }
     //move target function
     void MoveTargetPosition(){
         Debug.Log ("my position" + TargetPosition);
         transform.position = new Vector2(TargetPosition.x, this.TargetPosition.y);
         }
 }
 

Please help and thanx 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
1

Answer by Backside2357 · Dec 29, 2017 at 12:15 PM

Maybe the Vector3.Lerp(vectorA, vectorB, t) function is what you are looking for? The parameter t is a value between 0 and 1, where the Lerp function returns vectorA for t=0 and vectorB for t=1 and something inbetween (linear) for inbetween t values. The t value could then be computed using Time functions.

Comment
Add comment · Show 5 · 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 husnain_rao · Dec 29, 2017 at 12:54 PM 0
Share

can u please explain using my variable?

avatar image Backside2357 husnain_rao · Dec 29, 2017 at 11:08 PM 0
Share

Well I would delete the $$anonymous$$oveTargetPosition function and ins$$anonymous$$d would set a bool value where the function was called:

 do$$anonymous$$ove = true;


And also set the variable t to 0:

 t = 0;


It would also be necessary to make a copy of the current position:

 CurrentPosition = transform.position;


Then, in the standard $$anonymous$$onoBehaviour Update function you write something like this:

 if( do$$anonymous$$ove )
 {
 t += Time.deltaTime;
 if(t >= duration)
 {
 transform.position = TargetPosition;
 do$$anonymous$$ove = false;
 }
 else
 {
 transform.position = Vector2.Lerp(CurrentPosition, TargetPosition, t / duration);
 }
 }

duration is a constant you need to define and set to the value you want.

avatar image husnain_rao · Dec 30, 2017 at 09:28 AM 0
Share

Thanx For Your Time it Did'nt Worked. the object moves same like before. it jumps suddenly. i want to move the object smothly within a duration

avatar image Backside2357 husnain_rao · Jan 03, 2018 at 11:04 PM 0
Share

That is strange. But I found the time to create a simple example script:

  • Add a cube to your scene

  • Create a C# script with the name Smooth$$anonymous$$ove

  • Replace the content of the script with the code posted below

  • Attach the script to the cube

  • In Play$$anonymous$$ode, click the "Do $$anonymous$$ove" checkbox and play with the "Duration" value

  • Understand how the script works and adapt it to your needs

Code (Smooth$$anonymous$$ove .cs):

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Smooth$$anonymous$$ove : $$anonymous$$onoBehaviour {
 
     public bool do$$anonymous$$ove = false;
     public float duration = 2.0f;
 
     private float t = 0;
     private Vector3 originPosition;
     private Vector3 targetPosition;
 
     // Use this for initialization
     void Start () {
         originPosition = transform.position;
         targetPosition = originPosition + new Vector3(10, 0, 0);
     }
     
     // Update is called once per frame
     void Update () {
 
         if (do$$anonymous$$ove)
         {
             t += Time.deltaTime;
             if (t >= duration)
             {
                 transform.position = targetPosition;
                 do$$anonymous$$ove = false;
             }
             else
             {
                 transform.position = Vector3.Lerp(originPosition, targetPosition, t / duration);
             }
         }
         else
         {
             t = 0;
             transform.position = originPosition;
         }
 
     }
 }
 



avatar image husnain_rao Backside2357 · Jan 05, 2018 at 06:48 AM 0
Share

@Backside2357 Thanx For your time it hellped me. now the object is moving smoothly Thanx Again :)

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

240 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

Related Questions

How to All instantiated objects to change position if any object hits defined border 2 Answers

how do I use an Idle animation clip in a 2d blend tree? 0 Answers

Can't jump while running, and animation won't transition 0 Answers

Hello, help with animation between scenes 0 Answers

Transition Between Animations Delay 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