新建一个cube 作为地面。
新建一个Sphere
点击add component 选择刚体
新建脚本 Sphere
-
using System.Collections; -
using System.Collections.Generic; -
using UnityEngine; -
public class Shpere : MonoBehaviour { -
private Rigidbody rd;//刚体变量 -
private int force = 5;//力量 -
// Use this for initialization -
void Start () { -
rd = GetComponent(); -
} -
// Update is called once per frame -
void Update () { -
float h = Input.GetAxis("Horizontal"); -
float v = Input.GetAxis("Vertical"); -
rd.AddForce(new Vector3(h, 0, v)*5); -
} -
}
附加到sphere 上就可以控制小球运动了。根据方向键控制。
