Version: 2017.4
LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

Rigidbody2D.velocity

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Switch to Manual
public var velocity: Vector2;
public Vector2 velocity;

Description

Linear velocity of the rigidbody.

The velocity is specified as a vector with components in the X and Y directions (there is no Z direction in 2D physics). The value is not usually set directly but rather by using forces. Disable drag in the Inspector to stop the gradual decay of the velocity.

See Also: AddForce, drag, angularVelocity, Rigidbody.velocity.

no example available in JavaScript
//Create a GameObject and attach a Rigidbody2D component to it (Add Component > Physics2D > Rigidbody2D)
//Attach this script to the GameObject

//This script moves a GameObject up or down when you press the up or down arrow keys

using UnityEngine;

public class Example : MonoBehaviour { Rigidbody2D m_Rigidbody2D; float m_Speed;

void Start() { //Fetch the RigidBody from the GameObject m_Rigidbody2D = GetComponent<Rigidbody2D>(); //Set the GameObject’s speed to 10 m_Speed = 10.0f; }

void Update() { //Press the Up arrow key to move the RigidBody upwards if (Input.GetKey(KeyCode.UpArrow)) { //Move RigidBody upwards m_Rigidbody2D.velocity = Vector2.up * m_Speed; }

//Press the Down arrow key to move the RigidBody downwards if (Input.GetKey(KeyCode.DownArrow)) { //Move RigidBody downwards m_Rigidbody2D.velocity = Vector2.down * m_Speed; } } }