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
1
Question by stefan1294 · Feb 09, 2014 at 05:47 PM · c#animationgameobjectmoving

How to move a GameObject with an animation

I'd like to move my GameObject upstairs with an animation (just a moving animation, like it's smoothly moving up). However, I can't seem to figure out how this works. I've tried several methods, all of them are 'teleporting' (re-positioning) the GameObject. Here is my current code:

Some code may sound like bullshit, but it was all for testing purposes only

 Vector2 startPosition = new Vector2(pMenu.transform.position.x, pMenu.transform.position.y);
 
 Vector2 endPosition = new Vector2(pMenu.transform.position.x, 0.03f);
 
 pMenu.rigidbody2D.velocity = new Vector2(pMenu.rigidbody2D.velocity.x, 0);
 
 pMenu.transform.position = new Vector2(pMenu.transform.position.x, 0.03f);
 
 pMenu.transform.position = Vector2.Lerp(startPosition, endPosition, 5000);
 
 pMenu.transform.Translate(new Vector2(pMenu.rigidbody2D.velocity.x, 10));
 
 pMenu.transform.Translate(new Vector2(0, 500) * Time.deltaTime);

How can I move the gameobject up, so it's like.. sliding up to the desired Y coordinate?

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

Answer by robertbu · Feb 09, 2014 at 05:53 PM

Animation usually refers to Unity's Animation system. What you are trying to do is move something over time either by transform or by Rigidbody2D. Here are a couple of lines of code to get you started. Note that each one needs to be called repeatedly in Update().

 pMenu.transform.position = Vector3.Lerp(pMenu.transform.position, endPosition, speed * Time.deltaTime); 

or:

 pMenu.transform.position = Vector3.MoveTowards(pMenu.transform.position, endPosition, speed * Time.deltaTime);    

You might also take a look at iTween or the MoveObject script in the Wiki.

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 stefan1294 · Feb 09, 2014 at 06:46 PM 0
Share

Thanks, it works. However, how would I be able to detect if the object is on the correct Y-location? I tried an if-statement to check if the Y position of the object is in the Y-position I want it to be. But that doesn't work.

avatar image robertbu · Feb 09, 2014 at 09:22 PM 0
Share

In the absence of your specific code and a description of your goals, I cannot say. Be careful comparing floating point values directly. Given the way things are moved and calculated often specific values are skipped. Frequently you want to do a range check using greater-than and/or less-then than rather than a direct equals.

avatar image
4

Answer by mmeeks1 · Oct 26, 2017 at 11:03 AM

For the benefit of others who might stumble across this later, here is the solution that I came up with.

Create a new behavior/controller script to handle smooth motion and attach it to any/all objects that you want to translate smoothly around the scene. You can use the script I provide below. Once that is attached to the GameObject, simply call SetDestination on the object's GlideController and it will smoothly move to the specified location.

 using UnityEngine;
 
 public class GlideController : MonoBehaviour {
     public float speed;
 
     private Vector3 destination;
 
     void Start () {
         // Set the destination to be the object's position so it will not start off moving
         SetDestination (gameObject.transform.position);
     }
     
     void Update () {
         // If the object is not at the target destination
         if (destination != gameObject.transform.position) {
             // Move towards the destination each frame until the object reaches it
             IncrementPosition ();
         }
     }
 
     void IncrementPosition ()
     {
         // Calculate the next position
         float delta = speed * Time.deltaTime;
         Vector3 currentPosition = gameObject.transform.position;
         Vector3 nextPosition = Vector3.MoveTowards (currentPosition, destination, delta);
 
         // Move the object to the next position
         gameObject.transform.position = nextPosition;
     }
 
     // Set the destination to cause the object to smoothly glide to the specified location
     public void SetDestination (Vector3 value) {
         destination = value;
     }
 }
 
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

19 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

Related Questions

How can I animate between 3 gameObjects? 1 Answer

Moving gameobject with animation according to my script 0 Answers

Creating and Moving objects 2 Answers

How to move an object in a looped path but in random speed in 3D?, or How to move an object in circular path in random speed each time? 1 Answer

Button animation state stays Pressed or Highlighted after disabling GameObject 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