- Home /
Question by
jjoohhnnyy13 · Mar 02, 2019 at 12:23 PM ·
unity 53dmovement scriptautomatically
how to make automatic gradual acceleration
Hi, i need help, my programing looks ctrl+c and ctrl+v :D but when i read codes i understand relatively... this is my problem, i need give automatic gradual acceleration to code, which is below..
using UnityEngine; using System.Collections;
public class Mover : MonoBehaviour { public float speed; private Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
rb.velocity = new Vector3(0.0f, speed, 0.0f);
}
void FixedUpdate()
{
}
}
Comment
Best Answer
Answer by Ymrasu · Mar 02, 2019 at 02:23 PM
When people usually think of 'speed', they are actually referring to their max velocity. So instead of setting velocity to speed all at once, you'll need to slowly add to velocity. This is called acceleration.
float speed = 5f; // max velocity
float acceleration = 0.1f; // how fast to increase velocity
float velocity = 0f;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
// add to velocity
velocity += acceleration;
// check if we are at our top speed
if (velocity > speed) {
velocity = speed;
}
// then set it to rb
rb.velocity = new Vector3(0f, velocity, 0f);
}