Описание
Линейная скорость Rigidbody в единицах в секунду.
Скорость задается как вектор с компонентами в направлениях X и Y (в 2D-физике нет направления Z). Значение обычно устанавливается не напрямую, а с помощью сил. Отключите перетаскивание в Инспекторе, чтобы остановить постепенное уменьшение скорости.
Смотрите так же: AddForce, drag, angularVelocity, Rigidbody.velocity.
//Create a new 2D SpriteGameObject and attach this script to it.
//Этот скрипт перемещает GameObject вверх или вниз, когда вы нажимаете клавиши со стрелками вверх или вниз.
// Скорость устанавливается равной значению Vector2(). Если не изменить Vector2(),
//GameObject движется с постоянной скоростью.
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;
}
}
}
}