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 /
avatar image
1
Question by Jake 7 · May 23, 2011 at 12:36 AM · movementspeedaccelerationinertia

Character Movement

Hey, I'm making a 2d game and my character is a sphere. How can I make so that it takes like .5 seconds of holding A or D to make my character get to top speed? Right now it goes top speed the instant I press A or D. Also how can I make the character keep on moving the direction its going even if A or D is let go? Could I get some sample code? Thanks.

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

Answer by BinaryCaveman · May 23, 2011 at 02:01 AM

There could be several ways, depending on how your character is set up.

If you have a Rigidbody component attached to the character GameObject, you could use

 public var speed : float = 5;
 function Update()
 {
     rigidbody.AddForce(Vector3(
         Input.GetAxis("Horizontal") * speed,
         Input.GetAxis("Vertical") * speed,
         0));
 }

Read more about Rigidbody.AddForce() here.

You could also have a velocity vector which is updated as a key is pressed, like in this simple example (JavaScript):

 public var friction : float = 0.2;
 public var speed : float = 0.1;
 private var velocity = Vector3(0, 0, 0);
 function Update()
 {
     velocity.x += Input.GetAxis("Horizontal") * speed;
     velocity.y += Input.GetAxis("Vertical") * speed;
     
     transform.position += velocity;
     velocity *= friction;
 }

They both do basically the same thing, except one uses the built-in physics engine, and the other uses your own.

As for keeping the character moving in a direction for a longer time, you can increase the speed variable or decrease the Rigidbody's mass in the Rigidbody example, and reduce the friction or speed variable in the second example.

I hope this answers your question!


Additional code (see comments below):

 public var collisionParticles : ParticleEmitter;
 public var wall : Collider;
 function OnTriggerEnter(Collider other)
 {
     if (other == wall)
     {
         // instantiate a particle system in the current location with no rotation
         Instantiate(collisionParticles, transform.position, Quaternion.identity); 
         
         // destroy this GameObject
         Destroy(gameObject);
     }
 }
 
 @script RequireComponent(Rigidbody)

Comment
Add comment · Show 17 · 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 Jake 7 · May 24, 2011 at 12:56 AM 0
Share

Thanks for the info but my character runs on a character controller not a rigid body. Is there anyway to do this with a character controller?

avatar image BinaryCaveman · May 24, 2011 at 01:05 AM 1
Share

I see. $$anonymous$$y script still works in that case, but you could probably just attach a FPSInput Controller to your Character Controller (which will, in turn, add a Character $$anonymous$$otor, also). That would probably be a better solution.

avatar image Jake 7 · May 26, 2011 at 10:33 PM 0
Share

Alright thanks, I got it to accelerate but can I adjust it so it's for a 2d game? I really just want to move sideways and not in the z direction. Thank you for all you help so far.

avatar image Jake 7 · May 26, 2011 at 10:35 PM 0
Share

Also to let you get an idea of my game, it's very much like Red Ball, you can play it here http://www.king.com/games/sponsored-games/red-ball/ Thanks.

avatar image BinaryCaveman · May 27, 2011 at 12:37 AM 0
Share

I have updated my code above with the y axis ins$$anonymous$$d of the z. I moves the Input.GetAxis("Vertical") * speed in the Rigidbody example to the y value of the Vector3, and changed all of the z's to y's in the second example.

Show more comments
avatar image
1

Answer by Owen-Reynolds · May 30, 2011 at 05:37 PM

There is a window where you can change the arrow keys "speed up" and "coast" rates. Select Edit->ProjectSettings->Input. The Inspector will change. Pop open Axes and then Vertical. Sesitivity is how fast it speeds up, and gravity is coast speed.

Setting Sensitve/Grav to 0.2 will give 5 secs of holding UP arrow to reach full speed, and 5 secs to coast to 0. The range of "Vertical" is from -1 (backwards) to 1 (forwards.) So, the current value of 3 means it adds 3/second, getting to full speed in 1/3 of a second.

I've never tried it, but a Gravity of 0 (or really small) should make it coast forever.

Any script which uses Input.GetAxis("Vertical") uses these settings. Snap is also fun to look at, while the menu is open (it makes you have to slow down before changing directions.)

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to Tilt a Spaceship Based on Inertia/Acceleration Force Separate from Facing Direction? 2 Answers

Movement/Acceleration problems 1 Answer

Why are my wheels not moving at the same speed as my car? 0 Answers

Wheel rotation help cant work out the problem? 0 Answers

Rigidbody regenerated acceleration movement ? 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