'Why does a game object fall slowly when rigidbody2d.velocity.y is equal to 0?
I'm new to Unity.
rb2.velocity = new Vector2(speed * moveDirection, Y);
When I set the Y in new Vector() to 0, the character falls slowly because of (I think) the gravity. But when I set the Y to rb2.velocity.y, the character falls ordinarily.
Can you tell me the reason? Thx.
public class SecondCharacterController : MonoBehaviour
{
public float speed = 1.0f;
private float moveDirection;
private Rigidbody2D rb2;
private Animator anim;
private void Awake()
{
anim = GetComponent<Animator>();
}
private void Start()
{
rb2 = GetComponent<Rigidbody2D>();
}
private void FixedUpdate()
{
rb2.velocity = new Vector2(speed * moveDirection, rb2.velocity.y); // THIS LINE
}
private void Update()
{
if(Input.GetAxis("Horizontal") != 0) // Moving
{
if(Input.GetKey(KeyCode.A))
{
moveDirection = -1.0f;
anim.SetFloat("speed", speed);
}
else if(Input.GetKey(KeyCode.D))
{
moveDirection = 1.0f;
anim.SetFloat("speed", speed);
}
}
else // Idle
{
moveDirection = 0.0f;
anim.SetFloat("speed", 0.0f);
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

