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 Exalia · Nov 05, 2013 at 01:01 PM · physicsvelocityaddforcecontrolacceleration

Obtaining Constant Speed with AddForce

Hi, I've recently managed to get my character moving with AddForce However when the character starts moving the speed is slow and when I stop moving the character slides (obviously as it accelerates and decelerates)

I would like to make my starting velocity a multiple of direction.magnitude and the ending velocity zero.

Is this possible?

Thanks

Code :

         //On Screen Controls
         direction = new Vector3(joyScript.position.x, 0, joyScript.position.y);
         if (direction.magnitude > 0.1f)
         {
             Vector3 rightMovement = right * ps3speed * Time.deltaTime * joyScript.position.x;
             Vector3 upMovement = forward * ps3speed * Time.deltaTime * joyScript.position.y;
 
             Vector3 heading = Vector3.Normalize(rightMovement + upMovement);
             transform.forward = heading;
             //transform.position += rightMovement;
             //transform.position += upMovement;
             rigidbody.AddForce(rightMovement * direction.magnitude * speed);
             rigidbody.AddForce(upMovement * direction.magnitude * speed);
             rigidbody.AddForce(transform.forward * direction.magnitude * speed);
             playerModel.animation.Play("runforward");
             playerModel.animation["runforward"].speed = direction.magnitude;
         }
         else
         {
             playerModel.animation.CrossFade("idle");
         }


I'd eventually like to add the force on the player to the force of the bullet its producing so the player doesn't run into the bullet when it's fired.

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 robertbu · Nov 05, 2013 at 04:20 PM

You can set rigidbody.velocity to anything you want. Your AddForce() calls are a bit puzzling to me. I think you will want:

 Vector3 v3 = rightMovement + upMovement + transform.forward;
 v3 *= direction.magnitude * speed;
 rigidbody.velocity = v3;

Note if your code above is exectued every frame in update, then you'll need to make a substantial increase in 'speed'.

As an alternative, you can substantially increase the Rigidbody.drag and adjust the 'speed' to compensate. Depending on settings, you may have just a bit of acceleration/deceleration.

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 Exalia · Nov 06, 2013 at 10:17 AM 0
Share

$$anonymous$$Y code was a shambles I have to be honest, I'm working on implementing your suggestion as I type this I can't see why it wouldn't work so I'll make this the answer it does :)

Thanks

avatar image Exalia · Nov 06, 2013 at 11:20 AM 0
Share

I set the drag to allow for a constant horizontal velocity, is there a way to not let the drag effect my vertical velocity?

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

15 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

Related Questions

Accelerate a rigidbody towards max speed 2 Answers

How to make camera position relative to a specific target. 1 Answer

move 2d character affected by physics with velocity and/or add force 2 Answers

Why does writing to rigidbody.velocity after AddForce stop my rigidbody moving? 4 Answers

Character accelerates while running towards walls. "Sonic effect" 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