[Unity] Script: I tried to move game objects by “Rigidbody”.

To move game object in Unity, there are some ways.
The one is to use ‘Rigidbody’.
And also exist some ways for the ‘Rigidbody’.
This time, I tried following 4 ways.

[To specify the location to move in the flame]

1. position: completely teleportation

2. MovePosition: moving almost instantaneously
(possible to interpolate in the intermediate position)

[Add power or velocity to the object]

3. velocity: change the velocity in that moment

4. AddForce: add power in that moment

(Ref: https://docs.unity3d.com/ScriptReference/Rigidbody.html)

Let’s try!
Those gameobjects are moving with same velocity.
Blue: positionGreen: MovePositionRed: velocityyellow: AddForce

The setting of Rigidbody Componenet and C# program are followings.


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

public class MoveTest : MonoBehaviour {

    Rigidbody RB;
    Vector3 moveDirection;

    void Start () {
        moveDirection = new Vector3 (0, 1, 0);
        RB = this.GetComponent <Rigidbody> ();

        if (this.name == "Velocity")

            // Setting of "velocity"
            RB.velocity = moveDirection * 1.0f;

        else if (this.name == "AddForce")

            // Setting of "AddForce"
            RB.AddForce(moveDirection * 50.0f);

    void FixedUpdate() {

        if (this.name == "Position")

            // Setting of "position"
            RB.position = RB.position + moveDirection * 0.02f;
        else if (this.name == "MovePosition")

            // Setting of "MovePosition"
            RB.MovePosition (RB.position + moveDirection * 0.02f);

[Original Japanese Site: http://blog.lab7.biz/archives/2041666.html ]

Leave a Reply

Your email address will not be published. Required fields are marked *