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 navadeep2011 · Feb 13, 2013 at 12:22 PM · physicsacceleration

Linear Acceleration in unity 3d?

Hi Guys.. Please check this link and tell me the correct answer.

http://phet.colorado.edu/en/simulation/motion-2d

In above application how can i provide same linear acceleration in unity 3d? i followed http://docs.unity3d.com/Documentation/ScriptReference/Input-acceleration.html this link but ball can't move.. there is no change in object(sphere/ball) position. In this script i am getting dir=(0,0,0) so that i think ball can't moving.. please tell me the way how to do that.

Thanks to all my viewers.

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

4 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Setzer22 · Feb 13, 2013 at 12:34 PM

Well, linear acceleration is easy enough if you understand the physics behind it. Let's think first in scalar values and then this will be easily translated into vectors. First of all, acceleration is measured in distance over square time (m/s^2 in the metric system), and that means, a "meter per second" per second, so each second, you increase your speed by that amount. So, to implement a linear acceleration to an object in Unity, you can work easily with some basic equations and 3d Vectors to get the very same effect.

Your object has an speed vector, this vector has the direction and magnitude of its speed, the initial value for that vector should be, thus, your initial speed. Then, you might want to apply an acceleration to that vector, it's as easy as adding each frame a fixed value to that vector, in the direction you wish to have the acceleration. A common case for that is gravity, if you apply a constant acceleration downwards, you'll have a realistic gravity motion.

That's the problem, of course, of a frame not being a fixed amount of time, and that is solved with the class member Time.deltaTime, you can check out why does it work on the documentation, but basically multiplying your acceleration (and also speed) Vector by Time.deltaTime, you're translating the amounts in meters/frame to meters/second, which is a fixed amount of time. If you don't do that you'll have your character moving at different speeds on different computers, and even on the same computer the speed may vary on the CPU consume, which you definetly don't want.

Also, to a more specific implementation, if you're new to Unity, each object's position is stored in a Vector3 at its transform.position member (you can call this from whithin any monobehaviour script), modifying this vector over time will result in motion.

Hope it helped!

Comment
Add comment · Show 3 · 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 fafase · Feb 13, 2013 at 01:04 PM 0
Share

Your description of the delatTime is fine but irrelevant to the issue. When dealing with physics, you should use the FixedUpdate which has a deltaTime equivalent that is defined by the user so no need for deltaTime.

avatar image Setzer22 · Feb 13, 2013 at 02:03 PM 0
Share

I don't think this is irrelevant when directly modifying the transform. If he was asking specifically for the physics engine, you're right, otherwise the use of time.deltaTime and modifying the transform would be a lot more performance-efficient than using the phyisics engine to calculate a simple accelerated body. I've seen the java example that he posted and I don't think there's need to work with the physics engine at all to accomplish that.

avatar image fafase · Feb 13, 2013 at 02:36 PM 0
Share

Ok, I could not see the example he linked.

avatar image
0

Answer by fafase · Feb 13, 2013 at 01:04 PM

If you want to create acceleration use:

  rigidbody.AddForce(direction*speed,ForceMode.Acceleration);

Using the default one will not do as forces will get added each frame resulting in warp speed within a few second. A simple script like this should get you started:

 public class Script : MonoBehaviour {
     Rigidbody rig;
     float move;
     float speed = 70;
     void Start () {
         rig = GetComponent<Rigidbody>();
         gameObject.renderer.material.color = Color.blue;
     }
     void Update(){
         move = Input.GetAxis("Vertical");
     }
     void FixedUpdate () {
         print (rig.velocity.magnitude);
         if(move != 0)
             rig.AddForce(transform.forward*speed,ForceMode.Acceleration);
     }
 }

This will get your object moving like a really simplified car.

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
avatar image
0

Answer by navadeep2011 · Feb 13, 2013 at 08:56 PM

That is really helped me a lot.. please give me one more clarification on that how will i provide arrows of velocity and acceleration like same link in play mode ?

Comment
Add comment · 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 Setzer22 · Feb 13, 2013 at 10:41 PM 0
Share

You could just create an arrow asset, and then vary its scale with the acceleration and speed vectors' magnitude. Also, you should modify its rotation to the same rotation as the speed and acc vectors. Let me break down a little bit more that for you:

So, to get it done you should vary the localScale of the arrow in the desired axis, depending on the magnitude(which you can calculate with the Vector3.$$anonymous$$agnitude() method). You'll need to multiply that magnitude by some coeficient so it scales appropiately.

avatar image
0

Answer by Elecman · Sep 29, 2014 at 09:19 PM

If you want to visualize an acceleration vector, use the LinearAcceleration() function here:

http://wiki.unity3d.com/index.php/3d_Math_functions

and then use Debug.DrawRay() to draw a line made from the output vector.

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

12 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

Related Questions

how should the acceleration of an object sliding down a slope change over time? 1 Answer

Acceleration and Inertia in Top-Down 2D Game (C#) 2 Answers

add acceleraction to ship controls 0 Answers

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

Where can I find details on the physics engine? 2 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