level 1
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ObjectController : MonoBehaviour
{
public float moveSpeed = 5f;
public Color[] colors;
private int currentColorIndex = 0;
private void Update()
{
// 移动物体
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
transform.Translate(new Vector3(moveHorizontal, moveVertical, 0) * moveSpeed * Time.deltaTime);
// 更改物体颜色
if (Input.GetKeyDown(KeyCode.Space))
{
ChangeColor();
}
}
private void ChangeColor()
{
Renderer renderer = GetComponent<Renderer>();
// 切换到下一个颜色
currentColorIndex = (currentColorIndex + 1) % colors.Length;
// 设置新颜色
网页链接 = colors[currentColorIndex];
}
}
2023年09月20日 16点09分




