- Home /
Applying velocity to non rigid body object? By math?
Hello! Is there any way to simulate velocity on camera in scene? Without gravity, but with something like linear drag (air resistance), for smooth slowdown? There should be a way to do that :/ Only by script, without physics engine components...
Answer by robertbu · Feb 04, 2014 at 11:32 PM
Here is a example script to get you started. Attach it to a game object in a new scene and use the arrow keys (assuming that is how you have "Horizontal" and "Vertical" setup) to move the object:
#pragma strict
public var velocityFactor = 0.01;
public var dragFactor = 0.975;
public var maxVelocity = 1.5;
private var velocity : Vector3 = Vector3.zero;
function FixedUpdate(){
velocity += Vector3(Input.GetAxis("Horizontal"), 0.0, Input.GetAxis("Vertical")) * velocityFactor;
velocity *= dragFactor;
velocity = Vector3.ClampMagnitude(velocity, maxVelocity);
transform.position += velocity;
}
Answer by rutter · Feb 04, 2014 at 11:34 PM
Every GameObject has a Transform component which tracks its position, scale, and rotation. It's easy enough to change those values. The harder part is changing them in a way that models the behavior you want.
For example, this would roughly simulate gravity:
public class Falling : MonoBehaviour {
public Vector3 constantGravity = Vector3.down * 9.8f;
public Vector3 initialVelocity = Vector3.zero;
Vector3 velocity;
void Start() {
velocity = initialVelocity;
}
void Update() {
velocity += constantGravity * Time.deltaTime; //v = v0 + at
transform.position += velocity * Time.deltaTime;
}
}
For the effects you described, you can probably get away with tracking acceleration and velocity which change once per frame. If your measurements are in units-per-second, multiplying by Time.deltaTime will keep your math divorced from frame rate.
As you might expect, this sort of thing is much easier if you have a basic understanding of physics and/or calculus.
Your answer
Follow this Question
Related Questions
Getting UI into the right position after releasing drag 1 Answer
Properties of a cemara from cliping planes 1 Answer
Objects close to camera jittering a few 1000 units away from origin 0 Answers
What is drag and velocity measured in? 3 Answers
3D. How to get 1 pixel to equal 1 degree camera FOV 0 Answers