リジッドボディの線形速度
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; } } }