Help us understand the problem. What is going on with this article?

[Unity]Rigidbodyの速度を表示する.

More than 3 years have passed since last update.

ボールの速度が欲しい時のスクリプトの書き方の例(Rigidbody)

Ball.cs
using UnityEngine;
using System.Collections;

public class Ball : MonoBehaviour {

    private Rigidbody _rigidbody;

    void Start(){
        // 対象の
        _rigidbody = this.GetComponent<Rigidbody> ();
    }

    void Update(){

        // 1. 速度の取得の例
        // Aキーを推したら
        if (Input.GetKeyDown (KeyCode.A)) {
            // 速度ベクトルを表示
            Debug.Log ("速度ベクトル: " + _rigidbody.velocity);

            // 速度を表示
            Debug.Log ("速度: " + _rigidbody.velocity.magnitude);
        }
    }

    //2. 速度の代入の例
    // 一定速度以上になったら動きを止める

    // 物理的な挙動のため,FixedUpdateを使う.
    void FixedUpdate(){
        // もし速度が5以上だったら
        if (_rigidbody.velocity.magnitude > 5f) {
            // 速度を0にする
            _rigidbody.velocity = Vector3.zero;
            // 重力を無効にする
            _rigidbody.useGravity = false;
            //回転を0にする
            _rigidbody.gameObject.transform.rotation = Quaternion.Euler (Vector3.zero);
        }
    }
}
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away