public Vector2 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; } } }