1、首先创建一个平面和两个小球,个其中一个小球命名为Player,另一个命名为enemy,并给他们赋予不同的颜色以示区别,如下图
2、编写脚本使Player移动
创建一个脚本命名为move,并赋予小球,代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class move : MonoBehaviour
{
public float speed = 6.0f;
public float gravity = -9.8f;
private CharacterController _charController;
// Start is called before the first frame update
void Start()
{
_charController = GetComponent<CharacterController>();//使用附加到相同对象上的其他组件
}
// Update is called once per frame
void Update()
{
float x = Input.GetAxis("Horizontal") * speed;
float z = Input.GetAxis("Vertical") * speed;
Vector3 movement = new Vector3(x, 0, z);