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 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.

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

SmoothDamp bug? 0 Answers

Can't make smooth rotation using joystick 0 Answers

Need help with my code 0 Answers

lerp works on (-6.5/y/z) but not on (+6.5/y/z) 0 Answers

Why is this not moving? 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