1.创建一个宽敞的平面。
2.添加小球模型,设置Rigidbody
3.给小球添加移动代码
public class move: MonoBehaviour
{
public Rigidbody rg;
// Start is called before the first frame update
void Start()
{
rg = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
Vector3 m = new Vector3(h, 0, v);
transform.rotation = Quaternion.LookRotation(m);
}
}
4.将地面tag更改为Ground。
5.创建跳跃代码,在代码中关联地面tag,设置各种参数。
public class jump : MonoBehaviour
{
public float moveSpeed = 5f;
// 旋转速度
public float rotateSpeed = 5f;
// 跳跃速度
public float jumpSpeed = 8f;
// 是否在地上
private bool isGround = true;
Vector3 moveAmount;
Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
}
private void FixedUpdate()
{
rb.MovePosition(rb.position + moveAmount);
}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "Ground")
{
isGround = true;
}
}
}