新建一个Cube物体,和一个代码"yidong",如图:
并把代码"yidong"拖拽到Cube物体身上。
打开代码"yidong"复制内容如图:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class yidong : MonoBehaviour
{
private Transform weizhi; //变换组件类型变量
void Start()
{
weizhi = gameObject.GetComponent<Transform>(); //当前游戏物体身上.获取Transform组件并用weizhi来保存它的引用
}
void Update()
{
if(Input.GetKey(KeyCode.W)) //获取键盘输入,键码为W键
{
weizhi.Translate(Vector3.forward * 0.1f, Space.World); //引用weizhi使用Translate移动(方向向前速度帧数乘以0.1,位置沿着世界坐标系)
}
//同理复制上面的代码
if (Input.GetKey(KeyC