end0tknr's kipple - web写経開発

太宰府天満宮の狛犬って、妙にカワイイ

unity c# + 物理エンジンによる 3D Objectの移動

先程のentryの修正版です。

3D Object に Rigidbody追加

Sphereを選択し「Add Component」クリック

更にPhysics → Rigidbody クリック

最後に質量や抵抗を調整

c# script

先程のentryにあったものを以下のように修正します。

n == 0 or 2500 のタイミングで力を加え、 0.5の抵抗がある為、その後、徐々に減速します。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NewBehaviourScript : MonoBehaviour {

    int n = 0;
    Rigidbody rb = null;

    void Start(){
        rb = GetComponent<Rigidbody>();
    }

    void Update() {
        if (n == 0) {
            rb.velocity = Vector3.zero;
            rb.angularVelocity = Vector3.zero;
            var v = new Vector3(0f, 0f, 200f);
            rb.AddForce(v); //力を加えます
        }

        n++;

        if (n == 2500) {
            rb.velocity = Vector3.zero;
            rb.angularVelocity = Vector3.zero;
            var v = new Vector3(0f, 1f, -200f);
            n = -2500;
            rb.AddForce(v); //力を加えます
        }
    }
}