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 boatkay · Aug 08, 2016 at 05:56 PM · rigidbody2dtransform.positiontransform.translate

Best way to move an object smoothly

Hello I am trying to make a simple space mobile game. and I want my spaceship to continuously move up and can swipe left or right to move it to either the left or right to avoid obstacles. Right now I am using rigidbody2d.velocity to continuously move the spaceship up. But it doesn't seem smooth and realistic at all. There seem to be so much breaking in the movement. So I was wondering what would be the best possible way to move the object smoothly. I would be glad if someone could post some code of how I can move the object more efficiently and smoothly. This is my code for moving the object.

 public class PlayerController : MonoBehaviour {
 
     Rigidbody2D rb;
     public float gameSpeed;
     // Use this for initialization
     void Start ()
     {
           rb = GetComponent <Rigidbody2D> ();
     }
     
     void FixedUpdate ()
     {
         MovePlayer ();
     }
 
 
     void MovePlayer ()
     {
         rb.velocity = (new Vector2 (rb.velocity.x, 1f)) * gameSpeed;
     }
 }
 

 
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

Answer by Rob2309 · Aug 09, 2016 at 12:54 AM

If you don't need any physics, you could just do the following:

 public float verticalSpeed;
 
 void Update()
 {
      transform.position += Vector3.up * verticalSpeed * Time.deltaTime;
 }

If you, however, need some physics, consider using RigidBody.AddForce(...) (Documentation here)

Comment
Add comment · Show 1 · 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 boatkay · Aug 11, 2016 at 05:37 AM 0
Share

@Rob2309 what do you mean by if I would not need physics. I would be needing collider and would also liked to move the player object from left to right. Do you think that inculude physics?

avatar image
1

Answer by MelvMay · Aug 09, 2016 at 08:44 AM

The physics system only updates the positions at the fixed-update interval, not each frame as that would be expensive and inconsistent.

You can however ask the Rigidbody2D to update the Transform position each frame using interpolation from the old position to the new (current) one:

http://docs.unity3d.com/ScriptReference/Rigidbody2D-interpolation.html

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

67 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

Related Questions

Moving an object smoothly over predetermined distance without defining begin and end points 0 Answers

[Solved] Linear movement speed, reset position 1 Answer

Moving walls in multiple places 1 Answer

How can I move a Kinematic Rigidbody2d along the x-axis? 1 Answer

How can I move an object between two positions, while it is on a rotating platform? 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