• 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
Question by $$anonymous$$ · Apr 11, 2014 at 03:42 AM · 2dvelocitygravityfloattorque

Can I emulate simple space-like gravity?

Hey! I have this script here, for a 2D side scrolling game. It is attached to my player object.


 using UnityEngine;
 using System.Collections;
 
 public class Movement : MonoBehaviour {    
 
     //Variables
     public float PlayerSpeed = 5f;
     public float PlayerRotation = 10f;
     public float JumpForce = 800f;
     public bool IsRight = false;
     public bool CanFlip = true;
 
     //Update Function
     void Update () {
                 //Move
                 float Move = Input.GetAxis ("Horizontal");
                 float MoveVertical = Input.GetAxis ("Vertical");
 
                 rigidbody2D.velocity = new Vector2 (MoveVertical * PlayerSpeed, rigidbody2D.velocity.x);
                 rigidbody2D.velocity = new Vector2 (Move * PlayerSpeed, rigidbody2D.velocity.x);
                 rigidbody2D.AddTorque (Move * PlayerRotation);
                 rigidbody2D.AddTorque (MoveVertical * PlayerRotation);
                 
                 //Flip
                 if (Move > 0 && !IsRight) {
                         Flip ();
                 } else if (Move < 0 && IsRight) {
                         Flip ();
                 }
                 
                 //Jump
 //                if (Input.GetKeyDown (KeyCode.W)) {
 //                    rigidbody2D.AddForce(new Vector2 (0, JumpForce));
 //                }
         }
 
     //Flip Function
     void Flip() {
         if (CanFlip) {
                         IsRight = !IsRight;
                         Vector3 Scale = transform.localScale;
                         Scale.x *= -1;
                         transform.localScale = Scale;
                 }
     }
 }



It works perfectly! I am able to move my character in all four directions, and a slight torque is applied. What I want to achieve is a way to make the gravity feel a little more floaty, like space. Even when he is not moving I would love to see a slight 'bobbing' around. Is there a fairly simple way to do this?

Comment

People who like this

0 Show 1
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 rileydabozo · May 04, 2014 at 12:34 AM 0
Share

I am trying the same thing. A bit of slow movement when not moving, but when you use the thrusters (It's a spaceship game) you will still float a little in the direction you went, even after done with thrusters.

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Jeff-Kesselman · May 04, 2014 at 12:41 AM

Well, you wouldn't bob in space. In space, you only move when a force is applied and keep moving in the same way until another force is applied.

You bob on the water because the water under you moves up and down. If you were neutrally bouyent in the air, then you might bob a bit according to wind currents.

So, with that out of the way, what you could do is give yourself a slight drag and then apply a random slight nudge force periodically.

If you are looking for ballon bob where you torque a bit but always return to "up and down", then you would set some light gravity and push a force against it to counterbalance. You would attach an additional child component to the bottom with some additional mass to make the balance off center.

Comment

People who like this

0 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 $$anonymous$$ · May 04, 2014 at 03:19 PM 0
Share

Thanks! Yeah, I wasn't going for a too realistic gravity. When the character is still, it just looks a little lifeless. Thanks for the input!

avatar image

Answer by johkar · May 04, 2014 at 01:10 AM

http://gamemechanicexplorer.com/#thrust-2

This is not Unity but the math behind it could eventually help you in this case.

Comment

People who like this

0 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

24 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

Related Questions

Assets/Scripts/PlayerController.cs(32,49): error CS0126: An object of a type convertible to `float' is required for the return statement 1 Answer

Need help with this script 0 Answers

Keep up with falling object - Touches 0 Answers

How do I get my character to keep his left and right momentum in the air while also disabling movement controls until grounded? 0 Answers

how to jump at fixed height but faster 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