Unity的C#編程_1_改變遊戲對象的位置
- 2020 年 7 月 10 日
- AI
- C/C++/C#
Change GameObject Position
- 在遊戲裡面新建一個遊戲對象,比如一個正方形
- 然後新建一個腳本文件 C# Script,拖拽到這個遊戲對象的 Inspector 窗口,成為該遊戲對象的組件
- 打開該腳本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
public Vector3 startPosition;
// 我們在 inspector 中可以直接修改 public 變數
// 這裡我們設置一個初始位置變數
// Start is called before the first frame update
void Start()
{
startPosition = new Vector3(4, 5, 0);
// 初始位置設置為(4,5,0)
transform.position = startPosition;
// 獲取當前位置,賦值到我們希望的位置
// 可以看到,這裡的 tranform 和 position 都是 Inspector 中的欄位
// 遊戲開始的時候,對象的位置會重置到(0,0,0)
}
// Update is called once per frame
void Update()
{
}
}