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 Ryanmstc · Apr 02, 2015 at 09:24 PM · gravityspeedplatformer

constant movement of a game object along x axis

I am creating a 2D game in which the player needs to jump onto different platforms or fall onto another and stay alive as long as possible. So, the character needs to move along the x axis and be applied to gravity. Also I would like to add a slow but constant speed multiplier. This is a lot to ask for but I never made a game before and my scripting skills are at novice level. Can anyone at least point me in the right direction?

Comment
Add comment · 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 maccabbe · Apr 02, 2015 at 09:32 PM 1
Share

http://unity3d.com/learn/tutorials/modules/beginner/scripting

2 Replies

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

Answer by _joe_ · Apr 02, 2015 at 09:32 PM

Since you're using gravity (= physics), it's always advisable to stick to the control you can have from the RigidBody itself. It's very bad practice to translate rigidbodies.

So in your case, if you want to constantly move an object on the x axis, simply give him a velocity (knowing there are no drag values on the rigidbody) and he will keep on moving:

this.rigidbody.velocity = new Vector3(mySpeed,0,0);

where mySpeed is your variable that you can increment it slowly based on your multiplier (control it via time, or level, or score...)

Comment
Add comment · Show 5 · 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 Ryanmstc · Apr 02, 2015 at 09:56 PM 0
Share

Is this piece of script for C# or Java? Errors say (JavaScript): Property rigid body has been deprecated. Use GetComponent()ins$$anonymous$$d.> ~~~~~~ velocity is not a member of 'UnityEngine.Component'

Again, I am very new to this. Sorry if I frustrate you. I have added a 2d physics rigid body to my character (incase this is important to this error).

avatar image _joe_ · Apr 02, 2015 at 11:13 PM 0
Share

Yes in Javascript it's different. And of course using a RigidBody2D is different,

Here's the proper syntax (tested working).

 rigidbody2D.velocity = Vector2(mySpeed,0);

$$anonymous$$ake sure you have a rigidbody2D on your object and add the code to Start

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

avatar image Eric5h5 · Apr 02, 2015 at 11:26 PM 1
Share

It's not bad practice to translate rigidbodies; it's just not physically correct. Also your syntax is wrong and won't work in Unity 5; you need to use GetComponent.

avatar image _joe_ · Apr 02, 2015 at 11:47 PM 0
Share

you're totally right Eric thanks. And i haven't upgraded yet.

correction: GetComponent.().velocity = Vector2(mySpeed,0);

avatar image Ryanmstc · Apr 03, 2015 at 01:11 AM 0
Share

Thanks guys. Works Perfectly.

avatar image
0

Answer by Areshra · Oct 11, 2017 at 10:31 PM

i know its late but this may work to someone (c#)

 // public to change value on editor.    
        public float speed;
  
 // this is for storing your rigidbody use RigidBody2D for 2D game
     private Rigidbody object; 
     
     void Start () {
  //setting the value, use <Rigidbody2D> for 2D Game
         object = GetComponent<Rigidbody> ();
             }
         
         void Update () {
 // moving the object on x, you can choose direction (x,y,z) change to vector2 if its 2D (x,y)
         object.velocity = new Vector3 (speed, 0, 0); 
     }
 
 
     
 
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 Skibur · Oct 12, 2017 at 02:53 AM 0
Share

"object" is a protected keyword. Please use "obj" or "rb" ins$$anonymous$$d.

avatar image masonklaassenn · May 25, 2019 at 04:22 AM 0
Share

your amazing!! I know this is late but i had to say it

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Problem of gravity 1 Answer

How to reach a specific max speed with AddForce() ? 1 Answer

Playing with the Gravity PROBLEM 0 Answers

Constant speed when free-falling... Why?? 1 Answer

2D - Change jump direction 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