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 Psilocybe · Nov 24, 2015 at 03:40 AM · movement2d gamemovement script2d-physics

How to make a 2D object, which can be moved left and right, constantly fall downwards?

The game I'm working on is in 2D. I'm trying to get the player character, who can fly left & right, to be always gradually floating downwards (regardless of direction on X). I'm not too great at code, so any help would be very much appreciated. I just need help with the floating down part. The player starts off in the air.

Could it be as simple as just setting the gravity on the player to very, very low? (I can't test this until tomorrow)

I prefer Unityscript, but C# will do too.

Thank you.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by NinjaISV · Nov 24, 2015 at 03:43 AM

Just add a Rigidbody2D component to the gameobject then set its "Gravity Scale" to a really low number to make it fall slower. It falls slower because it's being affected less by gravity the lower its "Gravity Scale" is. In addition to falling down, if you want to move the object to the left and right, just add a script with this code to the object.

 using UnityEngine;
 using System.Collections;
 
 public class Player : MonoBehaviour {
     public float speed = 2f;
 
     Rigidbody2D rigidbody2d;
 
     void Awake () {
         rigidbody2d = GetComponent<Rigidbody2D> ();
     }
 
     void FixedUpdate () {
         if (Input.GetKey (KeyCode.LeftArrow))
             rigidbody2d.velocity = new Vector2 (speed * -1, rigidbody2d.velocity.y);
         if (Input.GetKey (KeyCode.RightArrow))
             rigidbody2d.velocity = new Vector2 (speed, rigidbody2d.velocity.y);
     }
 }

All this is doing is grabbing the Rigidbody2D component at the beginning of the game, then just changing its velocity based on what the player's pressing on the keyboard. You can even change how fast it moves left and right by changing the speed variable in the inspector.

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 Psilocybe · Dec 07, 2015 at 08:25 AM 0
Share

Yup, the gravity thing worked afterall. Thank you for taking the time to answer me.

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

37 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

Related Questions

Mouse controlled 2D object bleeding through walls 1 Answer

How to cancel the force caused by collision? So the player is not pushed away when it hits a corner? 0 Answers

NPC Movement 0 Answers

2D Change direction with an arc 1 Answer

How to give the player a limited amount of moves? 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