public Vector2 velocity ;

Description

Линейная скорость твердого тела.

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 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; } } }
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961