Linear velocity of the Rigidbody in units per second.
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.
//Create a new 2D Sprite GameObject and attach this script to it.
//This script moves a GameObject up or down when you press the up or down arrow keys. //The velocity is set to the Vector2() value. Unchanging the Vector2() keeps the //GameObject moving at a constant rate.
using UnityEngine;
public class ExampleClass : MonoBehaviour { private Rigidbody2D rb;
private float t = 0.0f; private bool moving = false;
void Awake() { rb = gameObject.AddComponent<Rigidbody2D>() as Rigidbody2D; rb.bodyType = RigidbodyType2D.Kinematic; }
void Start() { gameObject.GetComponent<SpriteRenderer>().sprite = Resources.Load<Sprite>("image128x128"); gameObject.transform.Translate(0.0f, 0.0f, 0.0f); }
void Update() { //Press the Up arrow key to move the RigidBody upwards if (Input.GetKey(KeyCode.UpArrow)) { rb.velocity = new Vector2(0.0f, 2.0f); moving = true; t = 0.0f; }
//Press the Down arrow key to move the RigidBody downwards if (Input.GetKey(KeyCode.DownArrow)) { rb.velocity = new Vector2(0.0f, -1.0f); moving = true; t = 0.0f; }
if (moving) { // Record the time spent moving up or down. // When this is 1sec then display info t = t + Time.deltaTime; if (t > 1.0f) { Debug.Log(gameObject.transform.position.y + " : " + t); t = 0.0f; } } } }
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.