玩家控制器脚本.cs using System.Collections; using System.Collections.Generic; using UnityEngine; public class playercone : MonoBehaviour { public KeyCode Up; //定义按键类型变量 public 修饰符可以在Unity内拖拽赋值获取组件与按键 public KeyCode Down; //private修饰符需要在代码内进行赋值获取组件与按键 public float speed; public Rigidbody2D rigidbody2d; //定义2D刚体变量 //private Rigidbody2D rigidbody2d; // 游戏开始是调用,进行游戏初始化 void Start() { //rigidbody2d = GetComponent<Rigidbody2D>(); //获取2D刚体组件 } // 每帧调用一次更新 void Update() { if (Input.GetKey(Up)) //Input.GetKey() //获取用户输入的按键 { rigidbody2d.velocity = new Vector2(0, speed); //施加一个力 类型为Vector2或者Vector3 } else if (Input.GetKey(Down)) { rigidbody2d.velocity = new Vector2(0, -speed); } else { rigidbody2d.velocity = new Vector2(0, 0); } } }