新建场景:创建Cube ,给物体添加Rigibody组件,创建Boxmove脚本挂在Cube上,如下设置:
Boxmove脚本代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Boxmove : MonoBehaviour {
Rigidbody rb;
float speed =2f;
float jumpforce;
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody>();
}
private void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
float jump = Input.GetAxis("Jump");
Vector3 movent = new Vector3(-moveVertical,0,moveHorizontal);
transform.Translate(movent *speed*Time.deltaTime);
rb.AddForce(0,jump*jumpforce,0);
}
}
这个脚本是根据下面的设置而来