LoginSignup
2
0

More than 5 years have passed since last update.

unity > Cubeを上下移動、左右回転 > AddForce() / AddTorque()

Last updated at Posted at 2015-08-01
動作確認
Unity 5.1.2 on MacOSX

Cubeを上下に移動したり、左右に回転させる。

using UnityEngine;
using System.Collections;

public class CubeControl : MonoBehaviour {

    public Rigidbody myRigid;

    void Start () {

    }

    void Update () {
        if (Input.GetKey (KeyCode.UpArrow)) {
            myRigid.AddForce(Vector3.up);
        }
        if (Input.GetKey (KeyCode.DownArrow)) {
            myRigid.AddForce(Vector3.down);
        }

        if (Input.GetKey(KeyCode.LeftArrow)) {
            myRigid.AddTorque(Vector3.up);
        }
        if (Input.GetKey(KeyCode.RightArrow)) {
            myRigid.AddTorque(Vector3.down);
        }                      
    }
}

ここで、「左回転」は操作者から見た左への回転とする。

x Size of torque along the world x-axis.
y Size of torque along the world y-axis.
z Size of torque along the world z-axis.

Shorthand for writing Vector3(0, 1, 0).

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0