- Home /
Making a character to glide
I am looking for a way to make my character to glide. So when a character jumps in the air, it will make him reach the ground at the lower speed. For example, some like this: http://www.youtube.com/watch?v=o5-V3zXAEsY
Answer by jayhatmaker · May 17, 2013 at 06:02 AM
I assume your talking about 2D game world? There are multiple ways to achieve this but here's one.
When glide is triggered, you can... reduce the downward vector by half.
*added you can possibly add a variable like
bool isGliding;
to your character. So user can hit certain key while jumping and etc. then you can reduce the downward vector.
Yes, the game is in 2d world. Where can I select glide? So I should use bool isGliding in my code?
Where can I select glide? So I should use bool isGliding in my code?
I believe there is no built in function to let your character glide. So you have to code the gliding behavior where your character movement is.
Is glide an option after jump only? or can a character glide by falling off from an edge?
If a character can only glide while jumping, your code should be in your jump code.
If your character can glide when s/he falls from a high place, and also in jump, you should create a function to check that your character is in air in other word, not on the ground.
It all depends on your code structure. It's a good practice to make a check list for an behavior like this.
character is in air?
Is a specific key hit? or glide automatically?
$$anonymous$$y plan is to have the character to glide when he jumps in the air. So the character can glide after they jump and fall from an edge.
At this point, I am confused a little. I have the character movement. So I am thinking would the code be something like this:
transform.Translate (Vector3.down* speed * Time.deltaTime);
So if the character is falling, they can hold down a button which would make them glide or fall down at a lower speed.
if you are making a script like that, you can just half the speed while gliding. or, you can set a fixed value and make another "glideSpeed" variable.
Thanks. One more thing, in the code, how I can I detect when the character is falling or jumping? So if the character is falling or jumping, it can be detected so the glide function can be in full effect. Is there a way to detect it? if so, how can I write it in the code?
Answer by ggpereira · Oct 08, 2020 at 01:21 AM
For all of you looking for a nice way, check the code below
using UnityEngine;
public class Glider : BaseBehaviour
{
/// <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
Making character to fly 1 Answer
controller.Move not working correctly 0 Answers