Last time, I tried to move game objects by “Rigidbody“.
(URL: http://blog-e.lab7sg.com/archives/84)
This time, I tried basic ways to move game objects by “Transform“.
[To specify the location to move in the flame]
1. position: Specify the position in a world space.
2. localPosition: Specify relative position from parent object
(If it doesn’t have the parent, it’s same as ‘position’.)
3. Translate: Move to specified direction and distance.
(Ref: https://docs.unity3d.com/ScriptReference/Transform.html)
Let’s try!
(Blue: position, Green: localPosition, Red: Translate)
This C# program is following.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveTest_2 : MonoBehaviour {
Vector3 moveDirection;
void Start () {
moveDirection = transform.up;
}
void FixedUpdate() {
if (this.name == "Position")
// Setting of position
transform.position
= transform.position + moveDirection * Time.deltaTime;
else if (this.name == "LocalPosition")
// Setting of localPosition
transform.localPosition
= transform.localPosition + moveDirection * Time.deltaTime;
else if (this.name == "Translate")
// Setting of Translate
transform.Translate(moveDirection * Time.deltaTime);
}
}
[Original Japanese Site: http://blog.lab7.biz/archives/2092278.html ]