- Home /
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.
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!
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.
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.
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.
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 ?
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.
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220613092620im_/https://answers.unity.com/themes/thub/images/avi.jpg)