- Home /
Make object fall with constant speed!
Hi is there a way to make a rigidbody that has gravity to fall with a constant speed? Preferably i want to increase or decrease the speed of falling.Thanks!
Answer by ByteSheep · May 14, 2013 at 09:38 PM
Well you could either avoid using the physics engine and instead set the transform directly, or you could perhaps set the rigidbodies velocity every frame.
Moving object position using transform:
function Update() {
transform.position -= transform.up * Time.deltaTime * 5;
}
Moving object by directly influencing the rigidbodys' velocity every frame:
function Update() {
rigidbody.velocity = Vector3(0, -5, 0);
}
Answer by TheDarkVoid · May 14, 2013 at 10:33 PM
You can set the rigidbody to Kinematic and use the transform to move the rigidbody around.
Answer by create3dgames · May 14, 2013 at 09:52 PM
You could use a transform.position.y and subtract from it every frame.
then it would not be constant as it would depend on Time.deltaTime
which changes every frame
Answer by ggpereira · Oct 08, 2020 at 01:10 AM
For all of you looking for a way, check the code below
using UnityEngine;
public class Glider : MonoBehaviour
{
/// <summary>
/// The speed when falling
/// </summary>
[SerializeField]
private float m_FallSpeed = 0f;
/// <summary>
///
/// </summary>
private Rigidbody2D m_Rigidbody2D = null;
// Awake is called before Start function
void Awake()
{
m_Rigidbody2D = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
if (IsGliding && m_Rigidbody2D.velocity.y < 0f && Mathf.Abs(m_Rigidbody2D.velocity.y) > m_FallSpeed)
m_Rigidbody2D.velocity = new Vector2(m_Rigidbody2D.velocity.x, Mathf.Sign(m_Rigidbody2D.velocity.y) * m_FallSpeed);
}
public void StartGliding()
{
IsGliding = true;
}
public void StopGliding()
{
IsGliding = false;
}
/// <summary>
/// Flag to check if gliding
/// </summary>
public bool IsGliding { get; set; } = false;
}
Then trigger the StartGliding method when you hold the jump button or whatever
Your answer
Follow this Question
Related Questions
Gravitational pull without losing speed 1 Answer
Fall Damage 3 Answers
RigidBody Question 1 Answer
Increase speed of an object's fall 1 Answer