• 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 Hollo1001 · Feb 12, 2016 at 03:42 PM · c#smoothmove an objectcs

Move Object in Button Click smooth to position?

I want to move an Object from one to a other position smoothly and with defined speed. Whit this code i only can set the position to that i want. But how can i moth it multiple times and smooth(over time).

     public void Clicked()
     {
           Vector3 Picposition = Pic.transform.position;
           Picposition.y -= 10f;
           Pic.transform.position = Picposition;
     }

Thx, Hollo1001

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

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by TreyH · Feb 12, 2016 at 07:20 PM

By the nature of your function name (Clicked: past tense, singular), you want a single process to be started and manage itself from the time of your click. The way to accomplish this is a Coroutine.

 void Clicked()
     {
         // Get the target position
         Vector3 relativeLocation = new Vector3(0, -10, 0);
         Vector3 targetLocation = Pic.transform.position + relativeLocation;
         float timeDelta = 0.05f;
 
         // Start your coroutine
         this.StartCoroutine(SmoothMove(targetLocation, timeDelta));
     }
 
     IEnumerator SmoothMove(Vector3 target, float delta)
     {
         // Will need to perform some of this process and yield until next frames
         float closeEnough = 0.2f;
         float distance = (Pic.transform.position - target).magnitude;
 
         // GC will trigger unless we define this ahead of time
         WaitForEndOfFrame wait = new WaitForEndOfFrame();
 
         // Continue until we're there
         while(distance >= closeEnough)
         {
             // Confirm that it's moving
             Debug.Log("Executing Movement");
 
             // Move a bit then  wait until next  frame
             transform.position = Vector3.Slerp(Pic.transform.position, target, delta);
             yield return wait;
 
             // Check if we should repeat
             distance = (Pic.transform.position - target).magnitude;
         }
 
         // Complete the motion to prevent negligible sliding
         Pic.transform.position = target;
 
         // Confirm  it's ended
         Debug.Log ("Movement Complete");
     }

edit: Changed transform.position to Pic.transform.position, as poster had.

Comment
Add comment · 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
0

Answer by wesleywh · Feb 12, 2016 at 04:45 PM

I would suggest looking into "Vector3.Lerp" here is the unity documentation about it:

http://docs.unity3d.com/ScriptReference/Vector3.Lerp.html

Here is how you would use it:

 transform.position = Vector3.Lerp(<FROM_POSITION>, <TO_POSITION>, <CALC_DISTANCE>);
 
 Inside <CALC_DISTANCE> this variable could account for your speed or a distance variable like they do in the example.

The example they provide is amazing. Click on the link and check it out. The Vector3.Lerp will smoothly transition from one point to another just like you want. This would most likely be called on an update function and have the "" be the current position of the person you are moving.

EX:

 this.transform.position

So here is a complete example that you could use:

 transform.position = Vector3.Lerp(transform.position, target.position, speed * Time.deltaTime);
Comment
Add comment · 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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

43 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

Related Questions

Need help with my code 0 Answers

transform.eulerAngles not working 2 Answers

Correctly smooth crouch script? 2 Answers

Moving moving Object with left/right arrow keys in a circular direction 3 Answers

Unity 2d make four directions (Up, Down, Right, Left. like RPG games) GUI buttons. Please help me. 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges