私的なプログラミングメモ - キー操作で移動

剛体

var accel = 1000.0;
var isJump = true;

function Update () 
{
   // x軸
   rigidbody.AddForce(
      transform.right * Input.GetAxisRaw( "Horizontal" ) * accel,
      ForceMode.Impulse
   );
   // z軸
  rigidbody.AddForce(
      transform.forward * Input.GetAxisRaw( "Vertical" ) * accel,
      ForceMode.Impulse
   );
   // y軸
   if (Input.GetKey ("space") && isJump == true) {
      //Jump();
      rigidbody.AddForce(
         transform.up * Input.GetAxisRaw( "Vertical" ) * accel,
         ForceMode.Impulse
      );
      isJump = false;
   }
      
// transform.up
}
  • rigidbody.AddForce()
意味
Forceその質量を使用して、rigidbodyへの継続的な力を追加します。
Accelerationその質量を無視して、rigidbodyへの継続的な加速を追加します。
Impulseその質量を使用して、rigidbodyに瞬時に速度変化を追加します。
VelocityChangeその質量を無視して、rigidbodyに瞬時に速度変化を追加します。
http://d.hatena.ne.jp/nakamura001/20120320/1332224...

剛体無視?(単なる平行移動?)

var speed = 100.0;

function Update () 
{
   var transX : float = Input.GetAxis ("Horizontal") * speed;
   var transZ : float = Input.GetAxis ("Vertical") * speed;
   transX *= Time.deltaTime;
   transZ *= Time.deltaTime;
   transform.Translate (transX, 0, transZ);
}