Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by gamenovice · Nov 23, 2013 at 02:39 AM · movementphysicstransformlerp

Unity 2D Scripted Movement

I was wondering how I could get it so that if I say:

WalkUp(3,5)

where 3 is the distance in meters and 5 is the time in seconds, that I could have an object move linearly and uniformly upward for 3 meters/5 seconds and have the method stop moving the character after that command, which may require a stationary Idle method.

Any insight would be great.

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
0
Best Answer

Answer by Tomer-Barkan · Nov 25, 2013 at 06:39 AM

In simple code without external libraries (sometimes I prefer my own code for simple things, easier to understand and debug than someone else's)

 public class ObjectMover {
     private Vector3 target;
     private Vector3 start;
     private float totalTime = 0; // when set to 0, will not move
     private float currentPosition = 0;
 
     public void Update() {
         if (totalTime > 0) {
             currentPosition += Time.deltaTime / totalTime; // advance position by the fraction of the total time that passed this last frame
             if (currentPosition >= 1) {
                 currentPosition = 1; // don't pass the target
                 totalTime = 0; // end the movement when reaching the target
             }
             transform.position = Vector3.Slerp(start, target, currentPosition); // position the object between the start and the target relative to the time passed out of the total time
         }
     }
 
     public void MoveTowards(Vector3 direction, float distance, float time) {
         target = transform.position + direction.normalized * distance; // set destination location
         totalTime = time; // set time to perform the movement
         if (time == 0) {
             transform.position = target;
         }
     }
 
     public void CancelMove() {
         totalTime = 0;
     }
 }

Not to use it, you would attach the ObjectMover script to any object you wish to be movable this way, and then call it this way to move it up 3 units in 5 seconds, as in your example:

 GameObject objectToMove = GameObject.Find("SomeObjectName");
 objectToMove.GetComponent<ObjectMover>().MoveTowards(Vector3.up, 3, 5);

Of course you can use object references instead of GameObject.Find.

Comment
Add comment · Show 2 · 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 gamenovice · Nov 30, 2013 at 08:30 AM 0
Share

sorry it took long to respond, I will definitely check this one out!

avatar image gamenovice · Jan 08, 2014 at 04:39 PM 0
Share

i applied it to a separate class of $$anonymous$$e, and it works like a charm!

avatar image
0

Answer by Eric5h5 · Nov 23, 2013 at 02:47 AM

MoveObject:

 function Start () {
     yield MoveObject.use.Translation(transform, Vector3.up*3, 5.0, MoveType.Time);
 }


Comment
Add comment · Show 2 · 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 gamenovice · Nov 23, 2013 at 02:53 AM 0
Share

is this able to work in a c# implementation?

avatar image Eric5h5 · Nov 23, 2013 at 04:34 AM 0
Share

Please read the usage instructions on the page I linked to.

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

18 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

Related Questions

How do I smooth the movement of three transforms moving at different rates along a single axis? 2 Answers

Using Add Force for character control 1 Answer

Learning to move 1 Answer

Is Physics.SyncTransforms() automatically called in built game? 0 Answers

stop vehicle from moving through walls 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