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
0
Question by S_Byrnes · Nov 25, 2015 at 01:33 PM · c#movementaxisforcetorque

How To Accelerate/Decelerate Gradually With Torque GetAxis

Hi guys, I'm really sorry to be doing this but I'm really lost and I've been searching Google and Unity Answers for a couple of weeks for the answer to this problem.

Basically I have a ball rolling game that I apply torque to move the player, it's using GetAxis Horizontal and Vertical, which I would have thought should have been easy to do this but everything I find on AddTorque or even AddForce for acceleration appears to only work for GetKey.insert arrow keys here and so-forth.

I'm needing a solution that uses GetAxis that essentially adds a good inertia to my acceleration and deceleration in any angle the axis can be pointed in at any given time.

Here is what I have, sorry I don't have a half-baked solution in there somewhere, I'm literally at a loss:

 void FixedUpdate()
     {
         if (onGround)
         {
 
             Vector3 torqueVector = (Input.GetAxis("Horizontal") * -mainCamera.transform.forward) + (Input.GetAxis("Vertical") * mainCamera.transform.right);
             GetComponent<Rigidbody>().AddTorque(torqueVector.normalized * accRate * Time.deltaTime, ForceMode.Acceleration);
 
         }
         else
         {
             dir.x = ((Input.GetAxis("Vertical") * flySpeed) * Time.deltaTime);
             dir.z = ((Input.GetAxis("Horizontal") * flySpeed) * Time.deltaTime);
 
             GetComponent<Rigidbody>().velocity += (mainCamera.transform.right * dir.z + new Vector3(mainCamera.transform.forward.x, 0, Camera.main.transform.forward.z) * dir.x);
 
             Vector3 torqueVector = (Input.GetAxis("Horizontal") * -mainCamera.transform.forward) + (Input.GetAxis("Vertical") * mainCamera.transform.right);
             GetComponent<Rigidbody>().AddTorque(torqueVector.normalized * accRate, ForceMode.Acceleration);
 
         }
     }

The 'else' part is for when it's in the air, the player is allowed to AddForce so they can move slightly in the air while also able to manipulate torque.

I hope one of you brilliant people out there can help me out, I'd be greatly appreciative!

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
0

Answer by centaurianmudpig · Nov 25, 2015 at 05:32 PM

I've done something similar, for rotating an object, using AddRelativeTorque based on mouse/keyboard input.

 float myDesiredSpeed = maxYawSpeed * yawRotation
 parentRigidbody.AddRelativeTorque(0, myDesiredSpeed * Mathf.Rad2Deg, 0, ForceMode.Impulse)

yawRotation is a value between 0 and 1.0, taken from the joystick/mouse. maxRotSpeed is the maximum rotation speed in Radians Per Second.

As you can guess this only works on a single rotation and you can add another axis easy enough or however you like.

Comment
Add comment · Show 10 · 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 S_Byrnes · Nov 25, 2015 at 11:48 PM 0
Share

Hey thanks for taking the time to answer!

I'm stuck again though, how can I have my vector3 input for axis be turned into a float?

Here is what I have so-far:

 Vector3 torqueVector = (Input.GetAxis("Horizontal") * -mainCamera.transform.forward) + (Input.GetAxis("Vertical") * mainCamera.transform.right);
 
                     float accRate = maxYSpeed * torqueVector;
 
                     GetComponent<Rigidbody>().AddTorque(0, accRate * $$anonymous$$athf.Rad2Deg, 0, Time.deltaTime, Force$$anonymous$$ode.Acceleration);
avatar image centaurianmudpig S_Byrnes · Nov 27, 2015 at 02:09 PM 0
Share

Input.GetAxis() is a float. Use it as is, rather than converting it to a vector.

avatar image S_Byrnes · Nov 28, 2015 at 05:06 AM 0
Share

Alright thanks, but how do I do that though?

I need to have this line work the same after using it as a float:

 Vector3 torqueVector = (Input.GetAxis("Horizontal") * -mainCamera.transform.forward) + (Input.GetAxis("Vertical") * mainCamera.transform.right);
avatar image centaurianmudpig S_Byrnes · Nov 28, 2015 at 11:03 AM 0
Share

Use variables to store the input.

 float horizontal = Input.GetAxis("Horizontal");
 float vertical = Input.GetAxis("Vertical");

Then use the variables as needed.

avatar image S_Byrnes centaurianmudpig · Nov 30, 2015 at 02:58 AM 0
Share

Thanks, I'm still not quite getting it though, what am I doing wrong here:

 float horizontal = Input.GetAxis("Horizontal");
                 float vertical = Input.GetAxis("Vertical");
 
                 float accRate = maxSpeed * horizontal;
                 GetComponent<Rigidbody>().AddTorque(0, accRate * -mainCamera.transform.forward * $$anonymous$$athf.Rad2Deg, 0, Force$$anonymous$$ode.Acceleration);
 
                 float accRate2 = maxSpeed * vertical;
                 GetComponent<Rigidbody>().AddTorque(0, accRate2 * -mainCamera.transform.right * $$anonymous$$athf.Rad2Deg, 0, Force$$anonymous$$ode.Acceleration);
Show more comments

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

32 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

Related Questions

Making a bubble level (not a game but work tool) 1 Answer

Distribute terrain in zones 3 Answers

Can't get character to Jump on the Y Axis (C#) 2 Answers

Simple 2-Button-Mash script 1 Answer

Rotating a player object without rotating the axis 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