一、创建一个unity项目
二、Create所需3Dobject
1.Player创建一个cube命名为player
2.walls为了使player不出界增设walls
三、添加属性:
1.添加在Player上
添加脚本,把代码拖到Player
添加new script组件,并命名为PlayMove,代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playMove : MonoBehaviour
{
public Rigidbody rd;
public float speadAutoMove=5;
public float speadMoveUpandDown=20;
// Start is called before the first frame update
void Start()
{
rd=gameObject.GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
PlayerAutoMove();
PlayerMoveUpandDown();
}
private void PlayerAutoMove(){
rd.AddForce(Vector3.right*speadAutoMove); //前进
}